int length():返回字符串的长度
char charAt(int index):返回某索引处的字符
@Testpublic void test03(){String str="HelloWorld";char c = str.charAt(0);System.out.println(c); //H}
boolean isEmpty():判断是否是空字符串
String toLowerCase():将String中的所有字符转换为小写 String toUpperCase():将String中的所有字符转换为大写
String trim():返回字符串的副本,忽略前部空格和尾部空格
boolean equals(Object obj):比较字符串的内容是否相同 boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写
String concat(String str):将指定字符串连接到此字符串的结尾。等价于用“+”。
@Testpublic void test03(){String str="HelloWorld";// char c = str.charAt(0);// System.out.println(c);String str1="!Java";String concat = str.concat(str1);System.out.println(concat); //HelloWorld!JavaSystem.out.println(str+str1); //HelloWorld!Java}
int compareTo(String anotherString):比较字符串的大小,从左到右比较每个字符,返回结果为字符所代表的ASCII值的相减的结果。
@Testpublic void test04(){String str="abc";String str1="abf";int i = str.compareTo(str1);System.out.println(i); //-3}
String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。
@Testpublic void test05(){String str="abcdrghnndfgggmm";String substring = str.substring(4);System.out.println(substring); //rghnndfgggmm}
String substring(int beginIndex,int endIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到endIndex的一个子字符串。
@Testpublic void test05(){String str="abcdrghnndfgggmm";String substring = str.substring(4,8);System.out.println(substring); //rghn}
boolean endsWith(String suffix):判断此字符串是否以指定的后缀结束 boolean startsWith(String suffix):判断此字符串是否以指定的前缀结束 boolean endsWith(String suffix,int toffset):判断此字符串从指定索引开始的子字符串是否以指定前缀开始
boolean contains(CharSequence s):当且仅当此字符串包含指定的char值序列时,返回true,可以放入字符串
@Testpublic void test06(){String str="abcdrghnndfgggmm";boolean a = str.contains("afff");System.out.println(a); //true}
int indexOf(String str):返回指定子字符串在此字符串中第一次出现的索引,未找到返回-1
@Testpublic void test07(){String str="HelloWorld";int llo = str.indexOf("llo");System.out.println(llo); //2}
int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出现的索引,从指定的索引开始,未找到返回-1
@Testpublic void test07(){String str="HelloWorld";int llo = str.indexOf("llo",5);System.out.println(llo); //-1}
int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现的索引,未找到返回-1,返回的索引还是从前往后的索引
@Testpublic void test08(){String str="HelloWorolld";int llo = str.lastIndexOf("llo");System.out.println(llo); //2}
int lastIndexOf(String str):返回指定子字符串在此字符串中最后一次出现的索引,从指定的索引开始反向搜索,未找到返回-1
@Testpublic void test08(){String str="HelloWllorolld";int llo = str.lastIndexOf("llo",9);System.out.println(llo); //6}
String replace(char oldChar,char newChar):返回一个新的字符串,它是用newChar替换此字符串中出现的所有oldChar得到的。 String replace(charSequence target,charSequence replacement):新字符串代替旧字符串
String replaceAll(String regex,String replacement):使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串。 String replaceFirst(String regex,String replacement):使用给定的replacement替换此字符串匹配给定的正则表达式的第一个子字符串。
Boolean matches(String regex):判断此字符串是否匹配给定的正则表达式。
String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。 String[] split(String regex,int limit):根据给定正则表达式的匹配拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。
