java:String

Common method

1, str.length (): returns the length of the string.

2, str.charAt (index): Returns the character string specified position.

3, str.indexOf (substr): returns the string in the first position substr occurs without -1.

4, str.indexOf (substr, index): Returns a string, starting at the specified position, the position of the first occurrence of substr not -1.

5, str.substring (index): Returns a string, the substring from index to the end.

6, str.substring (beginindex, endindex): Returns a string from beginIndex beginning, ending endindex substrings. For example: str.substring (2,5), returns 2--4.

7, str.compareTo (str1): Compares two strings, less than returns negative, returns a positive number greater than, equal to 0 returns.

8, str.equals (str1): compare two strings are equal, equal returns true, otherwise false.

9, str.toCharArray (): the string into a character array.

char c[] = str.toCharArray();
for(int i = 0; i < c.length; i++) {
    System.out.print(c[i] + " ");
}

10, str.split (): splitting the specified string delimiter.

(1) escape characters:, \ * is the escape character, you need to add \\ in front, for example: str.split ( "\\.").

(2) If there are a plurality of separators, need | connection.

String str = "a and b or c";
String s[] = str.split("and|or");
for(int i = 0; i < s.length; i++) {
    System.out.println(s[i]);
}

 

 

Published 150 original articles · won praise 4 · Views 6914

Guess you like

Origin blog.csdn.net/Napom/article/details/104197525