二、String类的常用方法
int length() :返回字符串的长度
@Test
public void MethodTest1 ( ) {
String s1 = "hello" ;
System. out. println ( s1. length ( ) ) ;
}
char charAt(int index) :返回字符串中指定索引处的字符
@Test
public void MethodTest2 ( ) {
String s1 = "hello" ;
System. out. println ( s1. charAt ( 0 ) ) ;
System. out. println ( s1. charAt ( 4 ) ) ;
System. out. println ( s1. charAt ( 5 ) ) ;
}
boolean isEmpty() :判断字符串是否为空,判断标准为当前数组的length
public boolean isEmpty ( ) {
return value. length == 0 ;
}
@Test
public void MethodTest3 ( ) {
String s1 = "hello" ;
System. out. println ( s1. isEmpty ( ) ) ;
String s2 = "" ;
System. out. println ( s2. isEmpty ( ) ) ;
String s3 = new String ( ) ;
System. out. println ( s3. isEmpty ( ) ) ;
}
String toLowerCase() :将String中所有字符转换为小写 String toUpperCase() :将String中所有字符转换为大写
@Test
public void MethodTest4 ( ) {
String s1 = "hello" ;
String s2 = s1. toUpperCase ( ) ;
String s3 = s2. toLowerCase ( ) ;
System. out. println ( s1) ;
System. out. println ( s2) ;
System. out. println ( s3) ;
}
String trim() :返回字符串的副本,忽略前导 空白和尾部 空白
@Test
public void MethodTest5 ( ) {
String s1 = " h e l l o " ;
String s2 = s1. trim ( ) ;
System. out. println ( "---" + s1 + "---" ) ;
System. out. println ( "---" + s2 + "---" ) ;
}
boolean equals (Object obj):比较字符串内容 是否相同 boolead equalsIgnoreCases (String anotherString):忽略大小写,比较内容
@Test
public void MethodTest6 ( ) {
String s1 = "hello" ;
String s2 = "Hello" ;
System. out. println ( s1. equals ( s2) ) ;
System. out. println ( s1. equalsIgnoreCase ( s2) ) ;
}
String concat (String str):将指定字符串连连接导此字符串的结尾,等价于“+”
@Test
public void MethodTest7 ( ) {
String s1 = "hello" ;
String s2 = s1. concat ( " world" ) ;
System. out. println ( s1) ;
System. out. println ( s2) ;
}
int compareTo (String anotherString):比较字符串大小,返回第一个不相同字符之间的差值
@Test
public void MethodTest8 ( ) {
String s1 = "abc" ;
String s2 = " abc" ;
System. out. println ( s1. compareTo ( s2) ) ;
}
String subString (int begiIndex):取现有字符串的子字符串,返回新字符串,该字符串从biginIndex开始截取到结尾 String subString (int beginIndex, int endIndex):返回新字符串,该字符串从biginIndex开始截取,到endIndex结尾(不包含endIndex所在字符)
@Test
public void MethodTest9 ( ) {
String s1 = "hello" ;
String s2 = s1. substring ( 1 ) ;
String s3 = s1. substring ( 1 , 4 ) ;
System. out. println ( s1) ;
System. out. println ( s2) ;
System. out. println ( s3) ;
}
boolean endsWith (String suffix):测试此字符串是否以指定后缀结束 boolean startsWith (String prefix):测试此字符串是否以指定前缀开始 boolean startsWith (String prefix, String toffest):测试此字符串是否以指定索引的子字符串开始
@Test
public void MethodTest10 ( ) {
String s1 = "hello" ;
System. out. println ( s1. endsWith ( "lo" ) ) ;
System. out. println ( s1. startsWith ( "Ho" ) ) ;
System. out. println ( s1. startsWith ( "ll" , 1 ) ) ;
System. out. println ( s1. startsWith ( "ll" , 2 ) ) ;
}
boolean contains (CharSequence s):当且仅当字符串包含指定的char值序列时,返回true
@Test
public void MethodTest11 ( ) {
String s1 = "hello" ;
System. out. println ( s1. contains ( "He" ) ) ;
System. out. println ( s1. contains ( "el" ) ) ;
}
int indexOf (String str):返回指定字符串在此字符串中第一次出现 的索引 int indexOf (String str, int fromIndex):返回指定子字符串在此字符串中从fromIndex开始从前往后第一次出现的索引 int lastIndexOf (String str):返回指定字符串在此字符串中最后一次出现 的索引 int lastIndexOf (String str, int fromIndex):返回指定元素str从fromIndex开始从后往前开始第一次出现的索引 indexOf和lastIndexOf方法如果未找到指定元素返回值都是-1
@Test
public void MethodTest12 ( ) {
String s1 = "hello world!" ;
System. out. println ( s1. indexOf ( "o" ) ) ;
System. out. println ( s1. indexOf ( "l" ) ) ;
System. out. println ( s1. indexOf ( "l" , 3 ) ) ;
System. out. println ( s1. lastIndexOf ( "l" ) ) ;
System. out. println ( s1. indexOf ( "ll" ) ) ;
System. out. println ( s1. lastIndexOf ( "ll" ) ) ;
System. out. println ( s1. lastIndexOf ( "o" , 3 ) ) ;
}
String replace (char oldChar, char newChar):返回一个新的字符串,将原字符串中的oldChar替换为newChar String replace (CharSequence target, CharSequence replacement):
@Test
public void MethodTest13 ( ) {
String s1 = "million" ;
String s2 = s1. replace ( 'm' , 'b' ) ;
String s3 = s1. replace ( "lion" , "k" ) ;
System. out. println ( s1) ;
System. out. println ( s2) ;
System. out. println ( s3) ;
}
String replaceAll (String regex, String replacement) regex: 正则表达式
@Test
public void MethodTest14 ( ) {
String str = ( "12hello34world5java7891mysql456" ) ;
String string1 = str. replaceAll ( "\\d+" , "," ) ;
String string2 = string1. replaceAll ( "^,|,$" , "" ) ;
System. out. println ( string1) ;
System. out. println ( string2) ;
}
String replaceFirst (String regex, String replacement)
@Test
public void MethodTest15 ( ) {
String str = ( "123def123" ) ;
String string = str. replaceFirst ( "\\d+" , "abc" ) ;
System. out. println ( string) ;
}
boolean matches (String regex):判断字符串中是否包含指定正则表达式中的字符
@Test
public void MethodTest16 ( ) {
String str = "12345" ;
boolean matches = str. matches ( "\\d+" ) ;
System. out. println ( matches) ;
String tel = "010-1234567" ;
boolean result = tel. matches ( "010-\\d{7,8}" ) ;
System. out. println ( result) ;
}
String[] split (String regex):根据给定正则表达式拆分字符串
@Test
public void MethodTest17 ( ) {
String str = "hello|world|java" ;
String[ ] strs = str. split ( "\\|" ) ;
for ( int i = 0 ; i < strs. length; i++ ) {
System. out. println ( strs[ i] ) ;
}
}
hello
world
java
String[] split (String regex, int limit):根据给定正则表达式拆分字符串,结果数组长度最大为limit
@Test
public void MethodTest18 ( ) {
String str = "hello|world|java|hello|world|java" ;
String[ ] strs = str. split ( "\\|" , 4 ) ;
for ( int i = 0 ; i < strs. length; i++ ) {
System. out. println ( strs[ i] ) ;
}
}
hello
world
java
hello|world|java