String 类+Excel版

String 类+Excel版

1) 概述:    

Java.lang包下的,final修饰

字符串是一个常量,也是一个字符串对象.

字符串一旦初始化,其值就不能发生改变.

        字符串的底层其实是一个字符数组.

String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现

String类是Java.lang包下的类不用导包.

 

  1. public class Demo1_String {
  2.    public static void main(String[] args) {
  3.       String str = "abc"; //"abc"可以看做一个字符串对象
  4.       str = "def";//当把"def"赋值给str,原来的"abc"就变成了垃圾
  5. "abc"代表对象,把地址值赋给str, "def"也是对象,把地址值又赋给了str
  6.       System.out.println(str);//打印的结果不是"类名@字符串地址",而是"abc",说明String重写了toString()方法
  7.  
  8.    }
  9.  
  10. }

 

2)构造方法(创建String类对象,参数用来赋值)

public String();

public String(String str);

public String(char[] chs);

public String(char[] chs,int offSet,int count);    //offSet代表开始的索引,count代表要拿几个

public String(byte[] bys);

public String(byte[] bys,int offSet,int count);    //offSet代表开始的索引,count代表要拿几个

public String(StringBuffer sb);

        public String(StringBuilder sb);

 

  1. public class Demo2_StringCon {
  2.    public static void main(String[] args) {
  3.       String s1 = new String();
  4.       System.out.println(s1);//(空串什么都没有)
  5.  
  6.       byte[] arr1 = {97,98,99};
  7.       String s2 = new String(arr1); //解码,将计算机读的懂的转换成我们读的懂(用的是当前项目默认的码表)(平台默认字符集)
  8.       System.out.println(s2);
  9.  
  10.       byte[] arr2 = {97,98,99,100,101,102};
  11.       String s3 = new String(arr2,2,3); //将arr2字节数组从2索引开始转换3个
  12.       System.out.println(s3);
  13.  
  14.       char[] arr3 = {'a','b','c','d','e'}; //将字符数组转换成字符串
  15.       String s4 = new String(arr3);
  16.       System.out.println(s4);
  17.  
  18.       String s5 = new String(arr3,1,3); //将arr3字符数组,从1索引开始转换3个
  19.       System.out.println(s5);
  20.  
  21.       String s6 = new String("kailing");
  22.       System.out.println(s6);
  23.    }
  24.  
  25. }

 

 

3)一般常用成员方法(总结Excel版)-需要Excel版的留言给我

 

4)String类的判断功能

A:String类的判断功能

  1. boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  2. boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  3. boolean contains(String str):判断大字符串中是否包含小字符串
  4. boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
  5. boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
  6. boolean isEmpty():判断字符串是否为空。

""null的区别

""是空字符串,同时也是String类的对象,可以调用String类的方法

Null是空常量,不能调用任何方法,否则会出现空指针异常

案例:

  1. public class Demo4_StringMethod {
  2.    public static void main(String[] args) {
  3.       //demo1();
  4.       //demo2();
  5.       String s1 = "kailing";
  6.       String s2 = "";
  7.       String s3 = null;没有记录任何字符串对象
  8.  
  9.       System.out.println(s1.isEmpty());
  10.       System.out.println(s2.isEmpty());
  11.       System.out.println(s3.isEmpty()); //java.lang.NullPointerException
  12.    }
  13.  
  14.    private static void demo2() {
  15.       String s1 = "我爱kailing,哈哈";
  16.       String s2 = "kailing";
  17.       String s3 = "baima";
  18.       String s4 = "我爱";
  19.       String s5 = "哈哈";
  20.  
  21.       System.out.println(s1.contains(s2)); //判断是否包含传入的字符串
  22.       System.out.println(s1.contains(s3));
  23.  
  24.       System.out.println("------------------");
  25.       System.out.println(s1.startsWith(s4)); //判断是否以传入的字符串开头
  26.       System.out.println(s1.startsWith(s5));
  27.  
  28.       System.out.println("------------------");
  29.       System.out.println(s1.endsWith(s4)); //判断是否以传入的字符串结尾
  30.       System.out.println(s1.endsWith(s5));
  31.    }
  32.  
  33.    private static void demo1() {
  34.       String s1 = "kailing";
  35.       String s2 = "kailing";
  36.       String s3 = "Kailing";
  37.  
  38.       System.out.println(s1.equals(s2)); //true
  39.       System.out.println(s2.equals(s3)); //false
  40.  
  41.       System.out.println("---------------");
  42.  
  43.       System.out.println(s1.equalsIgnoreCase(s2));
  44.       System.out.println(s1.equalsIgnoreCase(s3)); //不区分大小写
  45.    }
  46.  
  47. }

 

      

模拟用户登录

 

     需求:模拟登录,给三次机会,并提示还有几次。

     用户名和密码都是admin

案例:

  1. import java.util.Scanner;
  2. public class Test1 {
  3.    /
  4.       
  5.        需求:模拟登录,给三次机会,并提示还有几次。
  6.        用户名和密码都是admin
  7.        分析:
  8.        1,模拟登录,需要键盘录入,Scanner
  9.        2,给三次机会,需要循环,for
  10.        3,并提示有几次,需要判断,if
  11.     /
  12.    public static void main(String[] args) {
  13.       Scanner sc = new Scanner(System.in); //创建键盘录入对象
  14.  
  15.       for(int i = 0; i < 3; i++) {
  16.          System.out.println("请输入用户名:");
  17.          String userName = sc.nextLine(); //将键盘录入的用户名存储在userName中
  18.          System.out.println("请输入密码:");
  19.          String password = sc.nextLine(); //将键盘录入的密码存储在password中
  20.  
  21.          //如果是字符串常量和字符串变量比较,通常都是字符串常量调用方法,将变量当作参数传递,防止空指针异常
  22.          if("admin".equals(userName) && "admin".equals(password)) {
  23.             System.out.println("欢迎" + userName + "登录");
  24.             break; //跳出循环
  25.          }else {
  26.             if(i == 2) {
  27.                System.out.println("您的错误次数已到,请明天再来吧");
  28.             }else {
  29.                System.out.println("录入错误,您还有" + (2-i) + "次机会");
  30.             }
  31.          }
  32.  
  33.       }
  34.    }
  35.  
  36. }

 

实现方式二:

 

  1. /
  2.    
  3.     需求:模拟登录,给三次机会,并提示还有几次。
  4.     用户名和密码都是admin
  5.   @author JX
  6.    
  7.  /
  8. public class ScannerTest {
  9.    public static void main(String[] args) {
  10.       Scanner sc = new Scanner(System.in);
  11.       int count = 3;
  12.       while(true) {
  13.          System.out.println("请输入用户名:");
  14.          String userName = sc.nextLine();
  15.          System.out.println("请输入密码:");
  16.          String password = sc.nextLine();
  17.          if("admin".equals(userName)&&"admin".equals(password)) {
  18.             System.out.println("欢迎光临:"+userName);
  19.             break;
  20.          } else {
  21.             if(--count==0) {
  22.                System.out.println("登录次数已到,请明天再来吧!");
  23.                break;
  24.             } else{
  25.                System.out.println("您还有"+(count)+"机会!");
  26.             }
  27.          }
  28.       }
  29.    }
  30. }

 

 

5)String类的获取功能

A:String类的获取功能

  1. int length():获取字符串的长度。
  2. char charAt(int index):获取指定索引位置的字符
  3. int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
  4. int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
  5. int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  6. int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  7. lastIndexOf
  8. String substring(int start):从指定位置开始截取字符串,默认到末尾。
  9. String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。

 

案例:

  1. ublic class Demo5_StringMethod {
  2.    public static void main(String[] args) {
  3.       //demo1();
  4.       //demo2();
  5.       //demo3();
  6.       //demo4();
  7.       String s = "woaikailing";
  8.       s.substring(4); //subString会产生一个新额字符串,需要将新的字符串记录
  9.       System.out.println(s);//打印的是原来的s
  10.    }
  11.  
  12.    private static void demo4() {
  13.       String s1 = "kailingwudi";
  14.       String s2 = s1.substring(5);//默认到末尾
  15.       System.out.println(s2);
  16.  
  17.       String s3 = s1.substring(0, 5); //包含头,不包含尾,左闭右开
  18.       System.out.println(s3);
  19.    }
  20.  
  21.    private static void demo3() {
  22.       String s1 = "woaikailing";
  23.       int index1 = s1.indexOf('a', 3); //从指定位置开始向后找
  24.       System.out.println(index1);
  25.  
  26.       int index2 = s1.lastIndexOf('a'); //从后向前找,第一次出现的字符
  27.       System.out.println(index2);
  28.  
  29.       int index3 = s1.lastIndexOf('a', 7); //从指定位置向前找
  30.       System.out.println(index3);
  31.    }
  32.  
  33.    private static void demo2() {
  34.       String s1 = "kailing";
  35.       int index = s1.indexOf('e'); ????????????//参数接收的是int类型的,传递char类型的会自动提升
  36.       System.out.println(index);
  37.  
  38.       int index2 = s1.indexOf('z'); //如果不存在返回就是-1
  39.       System.out.println(index2);
  40.  
  41.       int index3 = s1.indexOf("ma"); //获取字符串中第一个字符出现的位置
  42.       System.out.println(index3);
  43.  
  44.       int index4 = s1.indexOf("ia");
  45.       System.out.println(index4);
  46.    }
  47.  
  48.    private static void demo1() {
  49.       //int[] arr = {11,22,33};
  50.       //System.out.println(arr.length); //数组中的length是属性
  51.       String s1 = "kailing";
  52.       System.out.println(s1.length()); //length()是一个方法,获取的是每一个字符的个数
  53.       String s2 = "你要减肥,造吗?";
  54.       System.out.println(s2.length());
  55.  
  56.       char c = s2.charAt(5); //根据索引获取对应位置的字符
  57.       System.out.println(c);
  58.       char c2 = s2.charAt(10); //StringIndexOutOfBoundsException字符串索引越界异常
  59.       System.out.println(c2);
  60.    }
  61.  
  62. }

    字符串的遍历

 

     需求:遍历字符串

案例:

  1. public class Test2 {
  2.    /
  3.       
  4.      需求:遍历字符串
  5.     /
  6.    public static void main(String[] args) {
  7.       String s = "kailing";
  8.  
  9.       for(int i = 0; i < s.length(); i++) { //通过for循环获取到字符串中每个字符的索引
  10.          /char c = s.charAt(i);
  11.          System.out.println(c);/
  12.          System.out.println(s.charAt(i)); //通过索引获取每一个字符
  13.       }
  14.    }
  15.  
  16. }

 

    

统计不同类型字符个数

 

     需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。

     ABCDEabcd123456!@#$%^

        案例:

          

  1. public class Test3 {
  2.    /
  3.        分析:字符串是有字符组成的,而字符的值都是有范围的,通过范围来判断是否包含该字符
  4.        如果包含就让计数器变量自增
  5.     /
  6.    public static void main(String[] args) {
  7.       String s = "ABCDEabcd123456!@#$%^";
  8.       int big = 0;
  9.       int small = 0;
  10.       int num = 0;
  11.       int other = 0;
  12.       //1,获取每一个字符,通过for循环遍历
  13.       for(int i = 0; i < s.length(); i++) {
  14.          char c = s.charAt(i); //通过索引获取每一个字符
  15.          //2,判断字符是否在这个范围内
  16.          if(c >= 'A' && c <= 'Z') {
  17.             big++; //如果满足是大写字母,就让其对应的变量自增
  18.          }else if(c >= 'a' && c <= 'z') {
  19.             small++;
  20.          }else if(c >= '0' && c <= '9') {
  21.             num++;
  22.          }else {
  23.             other++;
  24.          }
  25.       }
  26.  
  27.       //3,打印每一个计数器的结果
  28.       System.out.println(s + "中大写字母有:" + big + "个,小写字母有:" + small + "个,数字字符:"
  29.       + num + "个,其他字符:" + other + "");
  30.    }
  31.  
  32. }

 

 

6)String类的转换功能

A:String的转换功能:

  1. byte[] getBytes():把字符串转换为字节数组。(编码)
  2. char[] toCharArray():把字符串转换为字符数组。
  3. static String valueOf(char[] chs):把字符数组转成字符串。
  4. static String valueOf(int i):把int类型的数据转成字符串。
  5.     注意:String类的valueOf方法可以把任意类型的数据转成字符串
  6.  
  7. String toLowerCase():把字符串转成小写。(了解)
  8. String toUpperCase():把字符串转成大写。
  9. String concat(String str):把字符串拼接。

 

案例:

  1. import com.itkailing.bean.Person;
  2. public class Demo6_StringMethod {
  3.    public static void main(String[] args) {
  4.       //demo1();
  5.       //demo2();
  6.       //demo3();
  7.       String s1 = "kailing";
  8.       String s2 = "chengxuYUAN";
  9.       String s3 = s1.toLowerCase();
  10.       String s4 = s2.toUpperCase();
  11.  
  12.       System.out.println(s3);
  13.       System.out.println(s4);
  14.  
  15.       System.out.println(s3 + s4); //用+拼接字符串更强大,可以用字符串与任意类型相加
  16.       System.out.println(s3.concat(s4)); //concat方法调用的和传入的都必须是字符串
  17.    }
  18.  
  19.    private static void demo3() {
  20.       char[] arr = {'a','b','c'};
  21.       String s = String.valueOf(arr); //底层是由String类的构造方法完成的(底层是new对象来做的)
  22. valueOf(arr),是静态方法直接类名点调用即可,
  23.       System.out.println(s);
  24.  
  25.       String s2 = String.valueOf(100); //将100转换为字符串
  26.       System.out.println(s2 + 100);
  27.  
  28.       Person p1 = new Person("张三", 23);
  29.       System.out.println(p1);
  30.       String s3 = String.valueOf(p1); //调用的是对象的toString方法
  31.       System.out.println(s3);
  32.    }
  33.  
  34.    private static void demo2() {
  35.       String s = "kailing";
  36.       char[] arr = s.toCharArray(); //将字符串转换为字符数组
  37.  
  38.       for (int i = 0; i < arr.length; i++) {
  39.          System.out.print(arr[i] + "");
  40.       }
  41.    }
  42.  
  43.    private static void demo1() {
  44.       String s1 = "abc";
  45.       byte[] arr = s1.getBytes();
  46.       for (int i = 0; i < arr.length; i++) {
  47.          //System.out.print(arr[i] + " ");
  48.       }
  49.  
  50.       String s2 = "你好你好";
  51.       byte[] arr2 = s2.getBytes(); //通过gbk码表将字符串转换成字节数组
  52.       for (int i = 0; i < arr2.length; i++) { //编码:把我们看的懂转换为计算机看的懂得
  53.          //System.out.print(arr2[i] + " "); //gbk码表一个中文代表两个字节
  54.       } //gbk码表特点,中文的第一个字节肯定是负数
  55.  
  56.       String s3 = "";
  57.       byte[] arr3 = s3.getBytes();
  58.       for (int i = 0; i < arr3.length; i++) {
  59.          System.out.print(arr3[i] + "");
  60.       }
  61.    }
  62.  
  63. }

    按要求转换字符

 

     需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)

案例:

  1. public class Test4 {
  2.    /
  3.       
  4.      需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
  5.      链式编程:只要保证每次调用完方法返回的是对象,就可以继续调用
  6.     /
  7.    public static void main(String[] args) {
  8.       String s = "woaiKailingniaima";
  9.       String s2 = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());
  10.       System.out.println(s2);
  11.    }
  12.  
  13. }

 

 

把数组转成字符串

 

     需求:把数组中的数据按照指定个格式拼接成一个字符串

         举例:

             int[] arr = {1,2,3};    

         输出结果:

             "[1, 2, 3]"

案例:

  1. public class Test5 {
  2.  
  3.    /
  4.      分析:
  5.       1,需要定义一个字符串"["
  6.       2,遍历数组获取每一个元素
  7.       3,用字符串与数组中的元素进行拼接
  8.     /
  9.    public static void main(String[] args) {
  10.       int[] arr = {1,2,3};
  11.       String s = "["; //定义一个字符串用来与数组中元素拼接
  12.  
  13.       for (int i = 0; i < arr.length; i++) { //{1,2,3}
  14.          if(i == arr.length - 1) {
  15.             s = s + arr[i] + "]"; //[1, 2, 3]
  16.          }else {
  17.             s = s + arr[i] + ", "; //[1, 2,
  18.          }
  19.       }
  20.  
  21.       System.out.println(s);
  22.    }
  23. }

 

7)String类的其他功能

A:String的替换功能及案例演示

  1. String replace(char old,char new)
  2. String replace(String old,String new)

B:String的去除字符串两空格及案例演示

  1. String trim()

C:String的按字典顺序比较两个字符串及案例演示

  1. int compareTo(String str) 该比较基于字符串中各个字符的 Unicode 值
  2. 如果这两个字符串不同,那么它们要么在某个索引处的字符不同(该索引对二者均为有效索引),要么长度不同,
  3. 1. 如果它们在一个或多个索引位置上的字符不同,假设 k 是这类索引的最小值;则在位置 k 上具有较小值的那个字符串(使用 < 运算符确定),其字典顺序在其他字符串之前。在这种情况下,compareTo 返回这两个字符串在位置 k 处两个char 值的差,即值:this.charAt(k)-anotherString.charAt(k)
  4. 2.如果没有字符不同的索引位置,则较短字符串的字典顺序在较长字符串之前。在这种情况下,compareTo 返回这两个字符串长度的差,即值:this.length()-anotherString.length()
  5. (返回时int数,大就是正数,相等0,小于负数)(第一个一样就去比较第二个,) 按照码表值比较(char的转换都是Unicode,)
  6. int compareToIgnoreCase(String str)(了解)

案例:

  1. public class Demo7_StringMethod {
  2.    public static void main(String[] args) {
  3.       //demo1();
  4.       //demo2();
  5.       demo3();
  6.    }
  7.  
  8.    private static void demo3() {
  9.       String s1 = "a";
  10.       String s2 = "aaaa";
  11.  
  12.       int num = s1.compareTo(s2); //按照码表值比较
  13.       System.out.println(num);
  14.  
  15.       String s3 = "";
  16.       String s4 = "";
  17.       int num2 = s3.compareTo(s4);
  18.       System.out.println('黑' + 0); //查找的是unicode码表值
  19.       System.out.println('马' + 0);
  20.       System.out.println(num2);
  21.  
  22.       String s5 = "kailing";
  23.       String s6 = "KAILING";
  24.       int num3 = s5.compareTo(s6);
  25.       System.out.println(num3);
  26.  
  27.       int num4 = s5.compareToIgnoreCase(s6);
  28.       System.out.println(num4);
  29.    }
  30.  
  31.    private static void demo2() {
  32.       String s = " hei ma ";
  33.       String s2 = s.trim();
  34.       System.out.println(s2);
  35.    }
  36.  
  37.    private static void demo1() {
  38.       String s = "kailing";
  39.       String s2 = s.replace('i', 'o'); //用o替换i
  40.       System.out.println(s2);
  41.  
  42.       String s3 = s.replace('z', 'o'); //z不存在,保留原字符不改变
  43.       System.out.println(s3);
  44.  
  45.       String s4 = s.replace("ei", "ao");
  46.       System.out.println(s4);
  47.    }
  48.  
  49. }

 

    

 

字符串反转

 

     需求:把字符串反转

         举例:键盘录入"abc"        

         输出结果:"cba"

案例:

  1. import java.util.Scanner;
  2. public class Test6 {
  3.    /
  4.      分析:
  5.       1,通过键盘录入获取字符串Scanner
  6.       2,将字符串转换成字符数组
  7.       3,倒着遍历字符数组,并再次拼接成字符串
  8.       4,打印
  9.     /
  10.    public static void main(String[] args) {
  11.       Scanner sc = new Scanner(System.in); //创建键盘录入对象
  12.       System.out.println("请输入一个字符串:");
  13.       String line = sc.nextLine(); //将键盘录入的字符串存储在line中
  14.  
  15.       char[] arr = line.toCharArray(); //将字符串转换为字符数组
  16.  
  17.       String s = "";
  18.       for(int i = arr.length-1; i >= 0; i--) { //倒着遍历字符数组
  19.          s = s + arr[i]; //拼接成字符串
  20.       }
  21.  
  22.       System.out.println(s);
  23.    }
  24.  
  25. }

 

 

在大串中查找小串出现的次数    

     统计大串中小串出现的次数

案例:

  1. public class Test7 {
  2.    public static void main(String[] args) {
  3.       //定义大串
  4.       String max = "woaikailing,kailingbutongyubaima,wulunkailinghaishibaima,zhaodaogongzuojiushihaoma";
  5.       //定义小串
  6.       String min = "kailing";
  7.  
  8.       //定义计数器变量
  9.       int count = 0;
  10.       //定义索引
  11.       int index = 0;
  12.       //定义循环,判断小串是否在大串中出现
  13.       while((index = max.indexOf(min)) != -1) {
  14.          count++; //计数器自增
  15.          max = max.substring(index + min.length());
  16.       }
  17.       System.out.println(count);
  18.    }
  19.  
  20. }

 

 

  1. public class Test7 {
  2.    public static void main(String[] args) {
  3.       //定义大串
  4.       String max = "woaikailing,kailingbutongyubaima,wulunkailinghaishibaima,zhaodaogongzuojiushihaoma";
  5.       //定义小串
  6.       String min = "kailing";
  7.  
  8.       //定义计数器变量
  9.       int count = 0;
  10.       //定义索引
  11.       int index = 0;
  12.       //定义循环,判断小串是否在大串中出现
  13.       while(max.indexOf(min,index)!= -1) {
  14.          count++; //计数器自增
  15.          index = max.indexOf(min,index)+min.length();
  16.       }
  17.  
  18.       System.out.println(count);
  19.    }
  20. }

 

posted @ 2017-02-17 01:27  凯玲之恋  阅读(607)  评论(0)    收藏  举报