String类的判断功能、获取功能、
* String类的判断功能:
* boolean equals(Object obj):比较字符串的内容是否相同
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean startsWith(String str):判断字符串对象是否以指定的str开头
* boolean endsWith(String str):判断字符串对象是否以指定的str结尾
*
* String类的获取功能:
* int length():获取字符串的长度,其实也就是字符个数
* char charAt(int index):获取指定索引处的字符
* int indexOf(String str):获取str在字符串对象中第一次出现的索引
* String substring(int start):从start开始截取字符串
* String substring(int start,int end):从start开始,到end结束截取字符串。包括start,不包括end
*
* String类的转换功能:
* char[] toCharArray():把字符串转换为字符数组
* String toLowerCase():把字符串转换为小写字符串
* String toUpperCase():把字符串转换为大写字符串
* 字符串的遍历:
* A:length()加上charAt()
* B:把字符串转换为字符数组,然后遍历数组
*
* 去除字符串两端空格
* String trim()
* 按照指定符号分割字符串
* String[] split(String str)
*
//判断功能 public class String_PanDuan { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = "HELlo"; System.out.println(s1.equals(s3)); System.out.println(s1.equalsIgnoreCase(s3)); System.out.println(s1.startsWith("le")); System.out.println(s1.startsWith("he")); } }
输出
//获取功能 public class String_HuoQu { public static void main(String[] args) { String s = "helloworld"; System.out.println("s.length() = " + s.length()); System.out.println("s.charAt(0) = " + s.charAt(0)); System.out.println("s.charAt(1) = " + s.charAt(1)); System.out.println("s.indexOf("w") = " + s.indexOf("w")); System.out.println("s.indexOf("l") = " + s.indexOf("l")); System.out.println("s.substring(1,3) = " + s.substring(1,3));//包括start,不包括end System.out.println("s.substring(0) = " + s.substring(0)); } }
输出
//转换功能 public class String_ZhuanHuan { public static void main(String[] args) { String s = "abcde"; char[] chs = s.toCharArray(); System.out.println(chs); for(int i=0;i<chs.length;i++) { System.out.println(chs[i]); } System.out.println(""hello".toUpperCase() = " + "hello".toUpperCase()); System.out.println(""HellOWorld".toLowerCase() = " + "HellOWorld".toLowerCase()); } }
输出
public class String_FenGe { public static void main(String[] args) { String s1 = " hello "; String s2 = " hello world"; System.out.println("s1.trim() = "+"---" + s1.trim()+"---"); System.out.println("s2.trim() = "+"---" + s2.trim()+"---"); String s3 = "aa,bb,cc"; String[] strString = s3.split(","); System.out.println(strString); for(int i=0;i<strString.length;i++) { System.out.println(strString[i]); } } }