This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.
Description:
① All methods are public.
< return type >< method name (parameter list)>
Static int parseInt(String s)
ParseInt = static parseInt = static parseInt = static
1. char charAt(int index) :
Takes a character in a string where the parameter index refers to the ordinal number in the string. The ordinal number of the string starts at 0 through length()-1.
For example: String s = new String (” abcdefghijklmnopqrstuvwxyz “);
System.out.println(” s.charat (5): “+ s.charat (5));
The results are: S. Charat (5): F
2. Int compareTo(String anotherString) :
The current String object is compared to anotherString. Equality returns 0; If the two strings are not equal, the comparison starts at the 0th character and returns the difference of the first unequal character, or if the preceding part of a longer string happens to be a shorter string, returns the difference of their lengths.
3. Int compareTo(Object o) :
If o is a String, it does the same thing as 2; Otherwise, a ClassCastException is thrown.
For example :String s1 = new String(” abcdefghijklmn “);
String s2 = new String(” abcdefghij “);
String s3 = new String(” abcdefghiJALmn “);
System.out.println(” s1.compareTo(s2): “+ s1.compareTo(s2)); // Return the length difference
System.out.println(” s1.compareTo(s3): “+ s1.compareTo(s3)); // return the difference of ‘k’ -‘a ‘
The result is: s1.compareTo(S2): 4
s1.compareTo(s3): 10
4. String concat(String STR) :
Concatenate the String with STR.
5. Boolean contentEquals(StringBuffer sb) :
Compare the String object to the StringBuffer object sb.
6. Static String copyValueOf(char[] data) :
7. Static String copyValueOf(char[] data, int offset, int count) :
These two methods convert a char array to a String, similar to one of the constructors.
8. Boolean endsWith(String suffix) :
Whether the String ends in suffix.
For example: String s1 = new String(” abcdefghij “);
String s2 = new String(” ghij “);
System.out.println(” s1.endswith (s2): “+ s1.endswith (s2));
S1.endswith (s2): true
9. Boolean equals(Object anObject) :
Returns true if anObject is not empty and is the same as the current String; Otherwise, return false.
10. Byte [] getBytes () :
Convert the String to a Byte array.
Plenum (int srcBegin, int srcEnd, char[] DST, int dstBegin) :
This method copies a string into a character array. SrcBegin was the start place of copy, srcEnd was the end place of copy, DST was the target character array, dstBegin was the start place of copy of target character array.
For example: the char [] s1 = {‘ I ‘, ‘ ‘, ‘l’, ‘o’, ‘v’, ‘e’, ‘ ‘, ‘h’, ‘e’, ‘r’, ‘! ‘}; //s1=I love her!
String s2 = new String(” you! ); S2. GetChars (0, 3, s1, 7); //s1=I love you!
System.out.println( s1 );
I love you!
12. Int hashCode () :
Returns the hash table code for the current character.
13. Int indexOf(int ch) :
Only look for the first matching character position.
14. Int indexOf(int ch, int fromIndex) :
Start fromIndex to find the first character position.
15. Int indexOf(String STR) :
Only look for the first matching string position.
16. Int indexOf(String STR, int fromIndex) :
Start fromIndex to find the first matching string position.
For example: String s = new String(” Write once, run anywhere! );
String ss = new String(” run “);
System. The out. Println (” s.i ndexOf (” r “) : “+ s.i ndexOf (” r”));
System. The out. Println (” s.i ndexOf (” r “, 2) : “+ s.i ndexOf (” r”, 2));
System.out.println(” s.indexof (ss): “+ s.indexof (ss));
The result is: S.indexof (‘ r ‘): 1
S.i ndexOf (” r “, 2) : 12
s.indexOf(ss): 12
17. int lastIndexOf(int ch)
18. int lastIndexOf(int ch, int fromIndex)
19. int lastIndexOf(String str)
20. int lastIndexOf(String str, int fromIndex)
The above four methods are similar to 13, 14, 15, and 16, except that: Find the last matching content.
public class CompareToDemo {
public static void main (String[] args) {
String s1 = new String(” acBdebfg “);
System. Out.println (s1) lastIndexOf ((int) ‘b’, 7));
}
}
Running result: 5
FromIndex takes 7, the number of digits starting from the last character g of the string acbdebfg. If the match starts with character C, the last position to match b is found. So it’s 5.
21. Int length () :
Returns the current string length.
22. String replace(char oldChar, char newChar) :
Replace the first oldChar in the character string with newChar.
23. Boolean startsWith(String prefix) :
Whether the String starts with prefix.
24. Boolean startsWith(String prefix, int toffset) :
The String is counted from the toffset position, whether it starts with prefix.
For example: String s = new String(” Write once, run anywhere! );
String ss = new String(” write “);
String SSS = new String(” once “);
System.out.println(” s.startswith (ss): “+ s.startswith (ss));
System.out.println(” s.startswith (SSS,6): “+ s.startswith (SSS,6));
Result: s.startswith (ss): true
s.startsWith(sss,6): true
25. String subString (int beginIndex) :
Take the substring from beginIndex position to end.
26.String beginIndex (int beginIndex, int endIndex) :
Take a substring from beginIndex position to endIndex position.
27. Char [] toCharArray() :
Convert the String to a char array.
28. String toLowerCase () :
Converts a string to lowercase.
29. The String toUpperCase () :
Converts a string to uppercase.
For example: String s = new String(” java.lang.class String “);
System.out.println(” s.toupperCase (): “+ s.toupperCase ());
System.out.println(” s.tolowerCase (): “+ s.tolowerCase ());
The result is: s.topperCase (): java.lang.class STRING
s.toLowerCase(): java.lang.class string
30. static String valueOf(boolean b)
31. static String valueOf(char c)
32. static String valueOf(char[] data)
33. static String valueOf(char[] data, int offset, int count)
34. static String valueOf(double d)
35. static String valueOf(float f)
36. static String valueOf(int i)
37. static String valueOf(long l)
38. static String valueOf(Object obj)
The above method is used to convert various types to Java character types. These are class methods.