String9 commonly used method

1.indexOf (String str) Method Function: Get str index position in the whole of the first occurrence of the string
str: the string parameter, the meaning of the search string sub
Return Type: int
result Meaning: the first time position, if there have been no, or -1
case: in the "I love you China, I love you world" String word = "I love you China, I love you world";

int position = word.indexOf ( "summer school"); 
System.out.println (position); output -1

2.String trim () function method: box ends of the string is removed, the same space in the middle

Case:

String word2=" 我 爱 ";
String trim = word2.trim();
String result=trim.replaceAll(" ","");
System.out.println(result);

3.equalsIgnoreCase () method function: ignore case compare

Case:

public static void main(String[] args) {
String word="ABC你好dEF";
String newWord = word.toLowerCase();
System.out.println(newWord);
}

 4.concat () Function Method: string concatenation
Case:

public static void main(String[] args) {
String wordA="暑期班";

String wordB="无敌班";
String allWord = wordA.concat(wordB);
System.out.println(allWord);
}

5.args () Function Method: Split string

Case:

public static void main (String [] args) {
String Song = "1 @ a board children take on both sides, a low to a high. kids love it, one last look really fun. (playing a recreational facility) @ seesaw" ;
String [] = song.split words ( "@");
for (String Item: words) {
System.out.println (Item);
}

6.substring (int index) Method Function: extracting part of the string starting at a position index

Case:

String word = "I love you China, I love you world";
String result1 = word.substring (1, 3);
System.out.println (result1);

7.count () method function: Search for character occurrences

Case:

public static void main(String[] args) {
String str="我爱你中国,我爱你故乡";
String findStr="国";
String[] word=new String[str.length()];
for(int i = 0;i< word.length;i++) {
word[i]=str.substring(i,i+1);
}
int count=0;
for(String item : word) {
if(item.equals(findStr)) {
count++;
}
}
System.out.println(count);
}

8.lastIndexOf () method function: Find the value of the position

Case:

String word1 = "I love you China, I love you world";

int lastIndex = word1.lastIndexOf("爱你");
System.out.println(lastIndex);

9.length () method function: The total number of characters in the string statistics

 Case:

public static void main(String[] args){

String name = "Today is a good weather";

int length = name.length();

System.out.println(length);

}

Guess you like

Origin www.cnblogs.com/Rokem/p/11306130.html