The String class has a lot of methods, some of which I didn't know or didn't know, so I decided to put them together. It will be updated from time to time.
charAt(int index) Returns the char value for the specified index.
python
String test = "Aiue Okakikukeko";
//Returns the char value for the specified index.
System.out.println(test.charAt(0));
System.out.println(test.charAt(2));
System.out.println(test.charAt(5));
python
Ah
U
Or
codePointAt(int index) Returns the character (Unicode code point) at the specified index position.
python
String test = "Aiue Okakikukeko";
//Character at the specified index position(Unicode code point)Returns.
System.out.println(test.codePointAt(0));
System.out.println(test.codePointAt(1));
System.out.println(test.codePointAt(6));
python
12354
12356
12365
codePointBefore(int index) Returns the character (Unicode code point) before the specified index.
python
String test = "Aiue Okakikukeko";
//Character at the specified index position(Unicode code point)Returns.
System.out.println(test.codePointBefore(1));
System.out.println(test.codePointBefore(2));
System.out.println(test.codePointBefore(7));
python
12354
12356
12365
codePointCount(int beginIndex, int endIndex) Returns the number of Unicode code points in the specified text range for this String.
python
String test = "Aiue Okakikukeko";
//Returns the number of Unicode code points in the specified text range for this String.
System.out.println(test.codePointCount(0, 1));
System.out.println(test.codePointCount(2, 4));
System.out.println(test.codePointCount(3, 9));
python
1
2
6
compareTo(String anotherString) Compare two strings lexicographically.
python
String test = "Aiue Okakikukeko";
//Compares two strings lexicographically.
//Value 0 if the argument string is equal to this string
//A value less than 0 if the string is lexicographically smaller than the string argument
//Greater than 0 if lexicographically greater than the string argument
System.out.println(test.compareTo("Aiue Okakikukeko"));
System.out.println(test.compareTo("AIUEO"));
System.out.println(test.compareTo(""));
python
0
5
10
compareToIgnoreCase(String str) Compare two strings lexicographically, case insensitive.
python
String test = "ABCD";
//Compares two strings lexicographically, case-insensitive.
//Negative integer if the specified String is greater than this String, regardless of case.
//0 for the same, positive integer for less.
System.out.println(test.compareToIgnoreCase("abcd"));
System.out.println(test.compareToIgnoreCase("ABCD"));
System.out.println(test.compareToIgnoreCase("E"));
python
0
0
-4
concat(String str) Concatenates the specified string at the end of this string.
python
String test = "ABCD";
//Concatenates the specified string at the end of this string.
System.out.println(test.concat("E"));
System.out.println(test.concat("EWWWW"));
python
ABCDE
ABCDEWWWW
contains(CharSequence s) Returns true only if this string contains the specified sequence of char values.
python
String test = "ABCD";
//Returns true only if this string contains the specified sequence of char values.
System.out.println(test.contains("A"));
System.out.println(test.contains("a"));
System.out.println(test.contains("w"));
python
true
false
false
contentEquals(CharSequence cs) Compare this string with the specified CharSequence.
python
String test = "ABCD";
//Compares this string with the specified CharSequence.
System.out.println(test.contentEquals("ABCD"));
System.out.println(test.contentEquals("AC"));
System.out.println(test.contentEquals("a"));
python
true
false
false
contentEquals(StringBuffer sb) Compares this string with the specified StringBuffer.
python
String test = "ABCD";
//Compares this string with the specified StringBuffer.
System.out.println(test.contentEquals("A"));
System.out.println(test.contentEquals("ABCD"));
System.out.println(test.contentEquals("AD"));
System.out.println(test.contentEquals("s"));
python
false
true
false
false
endsWith(String suffix) Determines if this string ends with the specified suffix.
python
String test = "ABCD";
//Determines if this string ends with the specified suffix.
System.out.println(test.endsWith("D"));
System.out.println(test.endsWith("d"));
System.out.println(test.endsWith("A"));
python
true
false
false
equals(Object anObject) Compares this string with the specified object.
python
String test1 = "ABCD";
String test2 = "abcd";
//Compares this string with the specified object.
System.out.println(test1.equals(test2));
python
false
equalsIgnoreCase(String anotherString) Compare this String with another String, case insensitive.
python
String test1 = "ABCD";
String test2 = "abcd";
//Compares this string with the specified object.
System.out.println(test1.equals(test2));
python
true
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Copy characters from this character string to the copy destination character array.
srcBegin --Index of the first character in the string to be copied. srcEnd --The index after the last character in the string to be copied. dst --Destination array. dstBegin --Starting coordinates in the destination array.
The number of characters copied is srcEnd --srcBegin.
String Str1 = new String("ABCD");
char[] Str2 = new char[6];
//Copy characters from this string to the destination string array.
//Start index is 0
//The number of characters to be copied is 3
//Copy to Str2
//The start coordinate in the copy destination array is 0
Str1.getChars(0, 3, Str2, 0);
System.out.println(Str2);
Str1.getChars(2, 4, Str2, 0);
System.out.println(Str2);
python
ABC
CDC
indexOf(String str), indexOf(int ch) Returns the index of the position where the specified character first appears in this string.
python
String test1 = "ABCD";
//Returns the index of the position where the specified character first appears in this string.
System.out.println(test1.indexOf("C"));
System.out.println(test1.indexOf("A"));
System.out.println(test1.indexOf("W"));
python
2
0
-1
indexOf(String str, int fromIndex) Returns the index of the position where the specified substring first appears in this string after the specified index.
python
String test1 = "ABCD";
//Returns the index of the position where the specified substring first appears in this string after the specified index.
//From start position 0"C"What is the position where
System.out.println(test1.indexOf("C",0));
//From start position 2"A"What is the position where
System.out.println(test1.indexOf("A",2));
//From start position 3"D"What is the position where
System.out.println(test1.indexOf("D",3));
2
-1
3
isEmpty() Returns true only if length () is 0.
String test1 = "ABCD";
String str1 = "";
String str2 = null;
System.out.println(test1.isEmpty());
System.out.println(str1.isEmpty());
//NullPointerException
System.out.println(str2.isEmpty());
false
true
NullPointerException
lastIndexOf(int ch), lastIndexOf(String str) Returns the index of the position where the specified character last appears in this string.
String str = "AABBBCD";
//Returns the index of the position where the specified character last appears in this string.
//'A'Appears first at the end, so 1
System.out.println(str.lastIndexOf('A'));
//"B"Appears fourth at the end, so 4
System.out.println(str.lastIndexOf("B"));
//"U"Does not exist-1
System.out.println(str.lastIndexOf("U"));
1
4
-1
lastIndexOf(String str, int fromIndex) Returns the index of the position where the specified substring last appears in this string (the search starts at the specified index and goes first).
String str = "AABBBCD";
//Returns the index of the position where the specified substring last appears in this string
System.out.println(str.lastIndexOf("B",3));
System.out.println(str.lastIndexOf("B",2));
System.out.println(str.lastIndexOf("A",4));
3
2
1
length() Returns the length of this string.
String str = "AABBBCD";
//Returns the length of the string.
System.out.println(str.length());
7
substring(int beginIndex), substring(int beginIndex, int endIndex) Returns a string that is a substring of this string.
String str = "AABBBCD";
//Returns a string that is a substring of the string.
System.out.println(str.substring(2));
System.out.println(str.substring(5));
System.out.println(str.substring(2,5));
System.out.println(str.substring(3,6));
BBBCD
CD
BBB
BBC
toCharArray() Convert this string to a new string array.
String str = "AABBBCD";
char[] charAry = str.toCharArray();
//Converts a string to a new string array.
for(char i : charAry) {
System.out.println(i);
}
A
A
B
B
B
C
D
toLowerCase() Converts all characters in this String to lowercase using the rules of the default locale.
//Converts all characters in the String to lowercase.
System.out.println(str.toLowerCase());
aabbbcd
toUpperCase() Converts all characters in this String to uppercase using the rules of the default locale.
String str = "asdfgh";
//Converts all characters in the String to uppercase.
System.out.println(str.toUpperCase());
ASDFGH
trim() Returns a string whose value is this string (whitespace at the beginning and end is removed).
String str = " asdfgh ";
//A string with a value that removes leading and trailing whitespace from this string, or this string(If there are no leading and trailing spaces)。
System.out.println(str);
System.out.println(str.trim());
asdfgh
asdfgh
valueOf(boolean b) boolean Returns a string representation of the argument.
String str = " asdfgh ";
String str2 = "true";
//If the argument is true"true"Returns a string equal to, otherwise"false"Returns a string equal to.
System.out.println(Boolean.valueOf(str));
System.out.println(Boolean.valueOf(str2));
false
true
valueOf(int i) returns a string representation of the int argument.
String str = "asdfgh";
String str2 = "123456";
//NumberFormatException
System.out.println(Integer.valueOf(str));
System.out.println(Integer.valueOf(str2));
NumberFormatException
123456
valueOf(double d), valueOf(float f), valueOf(long l) Returns a string representation of the double, float, long arguments.
String str = "1234565";
System.out.println(Long.valueOf(str));
System.out.println(Float.valueOf(str));
System.out.println(Double.valueOf(str));
1234565
1234565.0
1234565.0
Recommended Posts