java常用类-String类

* 字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。
* 通过查看API,我们可以知道
* A:字符串字面值"abc"也可以看成是一个字符串对象。
* B:字符串是常量,一旦被赋值,就不能被改变。
*
* 构造方法:
* public String():空构造
* public String(byte[] bytes):把字节数组转成字符串
* public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
* public String(char[] value):把字符数组转成字符串
* public String(char[] value,int index,int count):把字符数组的一部分转成字符串
* public String(String original):把字符串常量值转成字符串
*
* 字符串的方法:
* public int length():返回此字符串的长度。

* String s = new String(“hello”)和String s = “hello”;的区别?
* 有。前者会创建2个对象,后者创建1个对象。
*
* ==:比较引用类型比较的是地址值是否相同
* equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。

* 看程序写结果
* 字符串如果是变量相加,先开空间,在拼接。
* 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。

 1         String s1 = "hello";
 2         String s2 = "world";
 3         String s3 = "helloworld";
 4         System.out.println(s3 == s1 + s2);// false
 5         System.out.println(s3.equals((s1 + s2)));// true
 6 
 7         System.out.println(s3 == "hello" + "world");// false 这个我们错了,应该是true
 8         System.out.println(s3.equals("hello" + "world"));// true
 9 
10         // 通过反编译看源码,我们知道这里已经做好了处理。
11         // System.out.println(s3 == "helloworld");
12         // System.out.println(s3.equals("helloworld"));

* String类的判断功能:
* boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串中是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
* boolean isEmpty():判断字符串是否为空。
*
* 注意:
* 字符串内容为空和字符串对象为空。
* String s = "";
* String s = null;

String s4 = "";
String s5 = null;
System.out.println("isEmpty:" + s4.isEmpty());
// NullPointerException
// s5对象都不存在,所以不能调用方法,空指针异常
System.out.println("isEmpty:" + s5.isEmpty());

* String类的获取功能
* int length():获取字符串的长度。
* char charAt(int index):获取指定索引位置的字符
* int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
* 为什么这里是int类型,而不是char类型?
* 原因是:'a'和97其实都可以代表'a'
* int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
* int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
* int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
* String substring(int start):从指定位置开始截取字符串,默认到末尾。
* String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。

 1         // 定义一个字符串对象
 2         String s = "helloworld";
 3 
 4         // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
 5         System.out.println("indexOf:" + s.indexOf('l', 4));
 6         System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
 7         System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
 8         System.out.println("----------------------");
 9 
10         // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
11         System.out.println("substring:" + s.substring(5));
12         System.out.println("substring:" + s.substring(0));
13         System.out.println("----------------------");
14 
15         // String substring(int start,int
16         // end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
17         System.out.println("substring:" + s.substring(3, 8));
18         System.out.println("substring:" + s.substring(0, s.length()));

字符串遍历:

1         for (int x = 0; x < s.length(); x++) {
2             // char ch = s.charAt(x);
3             // System.out.println(ch);
4             // 仅仅是输出,我就直接输出了
5             System.out.println(s.charAt(x));
6         }

判断该字符到底是属于那种类型的:

1             if(ch>='a' && ch<='z'){
2                 smallCount++;
3             }else if(ch>='A' && ch<='Z'){
4                 bigCount++;
5             }else if(ch>='0' && ch<='9'){
6                 numberCount++;
7             }

* String的转换功能:
* byte[] getBytes():把字符串转换为字节数组。
* char[] toCharArray():把字符串转换为字符数组。
* static String valueOf(char[] chs):把字符数组转成字符串。
* static String valueOf(int i):把int类型的数据转成字符串。
* 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
* String toLowerCase():把字符串转成小写。
* String toUpperCase():把字符串转成大写。
* String concat(String str):把字符串拼接。

* String类的其他功能:
*
* 替换功能:
* String replace(char old,char new)
* String replace(String old,String new)
*
* 去除字符串两空格
* String trim()
*
* 按字典顺序比较两个字符串
* int compareTo(String str)
* int compareToIgnoreCase(String str)

String类的compareTo方法的源码解析:

 1   private final char value[];
 2   
 3     字符串会自动转换为一个字符数组。
 4   
 5   public int compareTo(String anotherString) {
 6           //this -- s1 -- "hello"
 7           //anotherString -- s2 -- "hel"
 8   
 9         int len1 = value.length; //this.value.length--s1.toCharArray().length--5
10         int len2 = anotherString.value.length;//s2.value.length -- s2.toCharArray().length--3
11         int lim = Math.min(len1, len2); //Math.min(5,3); -- lim=3;
12         char v1[] = value; //s1.toCharArray()
13         char v2[] = anotherString.value;
14         
15         //char v1[] = {'h','e','l','l','o'};
16         //char v2[] = {'h','e','l'};
17 
18         int k = 0;
19         while (k < lim) {
20             char c1 = v1[k]; //c1='h','e','l'
21             char c2 = v2[k]; //c2='h','e','l'
22             if (c1 != c2) {
23                 return c1 - c2;
24             }
25             k++;
26         }
27         return len1 - len2; //5-3=2;
28    }
29    
30    String s1 = "hello";
31    String s2 = "hel";
32    System.out.println(s1.compareTo(s2)); // 2

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

 1     public static String arrayToString(int[] arr) {
 2         // 定义一个字符串
 3         String s = "";
 4 
 5         // 先把字符串拼接一个"["
 6         s += "[";
 7 
 8         // 遍历int数组,得到每一个元素
 9         for (int x = 0; x < arr.length; x++) {
10             // 先判断该元素是否为最后一个
11             if (x == arr.length - 1) {
12                 // 就直接拼接元素和"]"
13                 s += arr[x];
14                 s += "]";
15             } else {
16                 // 就拼接元素和逗号以及空格
17                 s += arr[x];
18                 s += ", ";
19             }
20         }
21 
22         return s;
23     }

 * 字符串反转

 1     public static String myReverse(String s) {
 2         // 定义一个新字符串
 3         String result = "";
 4 
 5         // 把字符串转成字符数组
 6         char[] chs = s.toCharArray();
 7 
 8         // 倒着遍历字符串,得到每一个字符
 9         for (int x = chs.length - 1; x >= 0; x--) {
10             // 用新字符串把每一个字符拼接起来
11             result += chs[x];
12         }
13         return result;
14     }

* 统计大串中小串出现的次数
* 举例:
* 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"
* 结果:
* java出现了5次

 1     public static int getCount(String maxString, String minString) {
 2         // 定义一个统计变量,初始化值是0
 3         int count = 0;
 4         
 5         int index;
 6         //先查,赋值,判断
 7         while((index=maxString.indexOf(minString))!=-1){
 8             count++;
 9             maxString = maxString.substring(index + minString.length());
10         }
11 
12         return count;
13     }

String的特点一旦被赋值就不能改变:

猜你喜欢

转载自www.cnblogs.com/samuraihuang/p/9861435.html