Java String commonly used method
-
length () of the length of the string
String a = "Hello Word!"; System.out.println(a.length);
Result output 10 is the string length.
-
the charAt () interception of a character
String a = "Hello Word"; System.out.println(a.charAt(1));
Output result is a string of characters is a subscript of e 1.
-
GetChars () taken by the plurality of other characters in the string receiving
String a = "Hello Word"; char[] b = new char[10]; a.getChars(0, 5, b, 0); System.out.println(b);
Result is output Hello, wherein the first parameter is the initial index to intercept the string (int sourceStart), the second parameter is a 5 index (int sourceEnd) to be taken after the end of the string i.e. the actual intercepted subscripts int sourceEnd-1, the third parameter is the received character string (char target []), the last parameter string was received reception start position.
-
the getBytes () string into a byte array
String a = "Hello Word"; byte b[] = a.getBytes(); System.out.println(new String(b));
Hello Word for result output byte array.
-
toCharArray () string into a character array
String a = "Hello Word"; char[]b = a.toCharArray(); System.out.println(b);
Hello Word output is the result of an array of characters.
-
the equals () and equalsIgnoreCase () Compares two strings are equal, the former case-sensitive, the latter does not distinguish between
String a = "Hello Word"; String b = "hello word"; System.out.println(a.equals(b)); System.out.println(a.equalsIgnoreCase(b));
The results for the first output is false, the second is true.
-
startsWith () and endsWith () determines if a string is beginning or end of a particular character
String a = "Hello Word"; System.out.println(a.startsWith("ee")); System.out.println(a.endsWith("rd"));
The results output from the first to false, the second is true.
-
the toUpperCase () and the toLowerCase () to convert a string uppercase or lowercase
String a = "Hello Word"; System.out.println(a.toUpperCase()); System.out.println(a.toLowerCase());
The first output result is "HELLO WORD", the second as "hello word".
-
the concat () connecting the two strings
String a = "Hello Word"; String b = "你好"; System.out.println(b.concat(a));
The resulting output is "Hello Hello Word".
-
TRIM () to remove the start and end of the space
String a = " Hello Word "; System.out.println(a.trim());
The resulting output is "Hello Word".
-
the substring () String taken
String a = "Hello Word"; System.out.println(a.substring(0, 5)); System.out.println(a.substring(6));
The first output result is "Hello", the first parameter 0 (beginIndex) is taken the start position, the second parameter 5 (endIndex) is taken to the end position, the second output result is "Word", parameter 6 (beginIndex) is the start position taken.
-
indexOf () and lastIndexOf () the former is to find the place where I first occurrence of a character or string, which is the place to find the last occurrence of a character or a string
String a = "Hello Word"; System.out.println(a.indexOf("o")); System.out.println(a.lastIndexOf("o"));
The first result output is 4, o is the first occurrence of the subscript, the second is 7, the last occurrence of the subscript o.
-
the compareTo () and compareToIgnoreCase () Size lexicographically compare two strings, the former case-sensitive, the latter does not distinguish between
String a = "Hello Word"; String b = "hello word"; System.out.println(a.compareTo(b)); System.out.println(a.compareToIgnoreCase(b));
The results output from the first to -32, to 0 second, the same size in the two strings in the dictionary order, 0 is returned.
-
the replace () Replace
String a = "Hello Word"; String b = "你好"; System.out.println(a.replace(a, b)); System.out.println(a.replace(a, "HELLO WORD")); System.out.println(b.replace("你", "大家"));
The results of the first output is "Hello", the second is "HELLO WORD", the third is "Hello everyone."