String String commonly used methods:
length()、equals()、startWith()、endsWith()、compareTo()、contains()、indexOf()、lastIndexOf()、substring()、trim();
A, public int length () is used:
By this method:
Gets a String object character sequence length (number of characters);
The following code ↗:
// public int length() 计算字符串的长度,字符个数
String word = "我在学习JavaSE";
System.out.println(word.length());
System.out.println("JavaSE的长度:"+"JavaSE".length());
Character sequence length: 1 counting from the beginning, is not 0;
Code output:
10
Length of JavaSE: 6
Two, public boolean equals (String s) used:
By this method:
Comparative String object if the current character sequence with the character sequence s is equal, regardless of their reference values, the output is equal to true, the output is not equal to false;
The following code ↗:
String wordz = "ITM" + "ITM" ;
String wordx = words + worda ;
//equals只判断两个字符序列的常量是否相等,不考虑对象引用是否相等
if(wordz.equals(wordx)){
System.out.println("hj == jh");
}else{
System.out.println("hj != jh");
}
// “ == ” 要判断两个字符序列的常量是否相等,也要判断对象引用是否相等,都相等才为true
if(wordz == wordx){
System.out.println("hj == jh");
}else{
System.out.println("hj != jh");
}
Meaning the code, == and equals (), it is not the same;
Code output:
== JH Hj
Hj! = JH
Three, public boolean startWith (String s) used:
By this method:
Comparing the current sequence of characters String object prefix is equal to the character sequence s, is equal to the output true, unequal output false;
The following code ↗:
String tom = "今天是个好天气";
System.out.println("判断前缀,startsWith:"+tom.startsWith("今天"));
Code output:
true
Four, public boolean endsWith (String s) of use:
By this method:
Comparing the current sequence of characters String object suffix is equal to the character sequence s, is equal to the output true, unequal output false;
The following code ↗:
String toms = "明天要出太阳了";
System.out.println("判断后缀,endsWith:"+toms.endsWith("太了"));
Code output:
false
Five, public int compareTo (String s) of use:
By this method:
Comparing two sequences string, case sensitive, with reference to the Unicode standard character size comparison table;
1, when the same character sequence s String object, the method returns 0;
2, when the character sequence is larger than s String object, the method returns a value;
3, when the character sequence smaller than s String object, the method returns a negative value;
The following code ↗:
//当 String 对象的字符序列与 s 的 相同,则方法返回 0 ;
String hellos = "abcd";
String hellox = "abcd";
String helloz = "ABCD";
System.out.println(hellos.compareTo(hellox));
System.out.println(hellos.compareTo(helloz));
//当 String 对象的字符序列比 s 的 大 ,则方法返回一个 正值 ;
String hellos = "abcd";
String hellox = "abc";
System.out.println(hellos.compareTo(hellox));
//当 String 对象的字符序列比 s 的 小 ,则方法返回一个 负值 ;
String hellos = "abc";
String hellox = "abcd";
System.out.println(hellos.compareTo(hellox));
Code output:
0
32
1
-1
Six, public boolean compareToIgnoreCase (String s) of use:
This method compareTo (String s) is the same, the only difference is that the method is not case-sensitive;
The following code ↗:
//当 String 对象的字符序列与 s 的 相同,则方法返回 0 ,不区分大小写 ;
String hellos = "abcd";
String helloz = "ABCD";
System.out.println(hellos.compareToIgnoreCase(helloz));
Code output:
0
Seven, public boolean contains (String s) of use:
By this method:
String object determines whether the current character sequence contains a sequence of characters parameter s, includes an output true, does not include an output false;
The following code ↗:
String Fa = "Hello you world";
System.out.println(Fa.contains("Hl"));
System.out.println(Fa.contains(" wo"));
Code output:
false
true
Eight, public int indexOf (String s), public int indexOf (String s, int startpoint) use:
1, a sequence of characters from the index position 0 String object to start retrieving the first occurrence of the character sequence parameter s, and returns the location;
2, when the corresponding character is not retrieved, the return value is -1;
3, the space character can be considered;
public int indexOf (String s) Method:
Code is as follows ↗:
//用于判断字符最先出现的位置
String cases = "hello are you,thank you";
System.out.println(cases.indexOf("ar"));
System.out.println(cases.indexOf("ae"));
Code output:
6
-1
public int indexOf( String s , int startpoint ) 方法:
Code is as follows ↗:
String cases = "hello are you,thank you";
System.out.println(cases.indexOf("llo",1)); //用于判断指定位置的字符
System.out.println(cases.indexOf("llo",11)); //用于判断指定位置的字符
Code output:
2
-1
Nine, public int lastIndexOf (String s) of use:
1, a sequence of characters from the index position 0 String object to begin retrieving character sequence parameter s last seen position, and returns the location;
The following code ↗:
String cases = "hello are you,thank you";
System.out.println(cases.lastIndexOf("o")); //用于判断字符最后出现的位置
System.out.println(cases.lastIndexOf("a")); //用于判断字符最后出现的位置
String casex = "https://fanyi.baidu.com//?aldtype=85&keyfrom";
System.out.println(casex.indexOf("//")); //用于判断字符最先出现的位置
System.out.println(casex.lastIndexOf("//")); //用于判断字符最后出现的位置
Code output:
21
16
6
23
Ten, public boolean substring (int startpoint) use:
By this method:
Counted from 0, startpoint position of a character sequence replication String object to a character in the last position to give a new String object;
The following code ↗:
String sub = "梦想着,怀念着,希望着" ;
String su = sub.substring(1);
System.out.println(su);
Code output:
Thinking, miss, hoping the
Eleven, public boolean substring (int start, int end) is used:
By this method:
Counted from 0, the sequence of characters in a String object copy start position to the end-1 character at a position to give a new String object;
The following code ↗:
String sub = "梦想着,怀念着,希望着" ;
String ss = sub.substring(1, 5); //5-1
System.out.println(ss);
String sx = sub.substring(4, 6); //6-1
System.out.println(sx);
Code output:
Thinking, pregnant
miss
Twelve, public boolean trim () is used:
trim () method is also obtained a new String object, but the character sequence new String object of the current sequence of characters String object before and after removing space characters in sequence;
The following code ↗:
String wordd = " and you,go to eat fish " ;
System.out.println(wordd+",Yes");
String wordds = wordd.trim();
System.out.println(wordds+",Yes");
Code output:
and you,go to eat fish ,Yes
and you,go to eat fish,Yes