String: String type
1. Constructor.
2. Method.
illustrate:
1. All methods are public;
2. Writing format: [Modifier] <Return type> <Method name ([Parameter list])>
like:
static int parseInt(String s) means: this method (parseInt) is a class method (static), the return type is (int), and the parameters required by the method are of type String.
1. Constructor.
String(): Construct an empty string object.
String(byte[] bytes): Construct a string object through a byte array.
String(byte[] bytes, int offset, int length): Construct a string object from a byte array, starting from offset, a total of length long bytes.
String(char[] value): Construct a string object through a char array.
String(char[] value, int offset, int count) : Construct a string object from a char array, starting from offset, a total of length long bytes.
String(String original): Construct a copy of original. Yes, copy an original.
String(StringBuffer buffer) : Construct a string object through a StringBuffer array;
byte[] b = {'a','b','c','d','e','f','g','h','i','j'};
char[] c = {'0','1','2','3','4','5','6','7','8','9'};
String sb = new String(b);
String sb_sub = new String(b,3,2);
String sc = new String(c);
String sc_sub = new String(c,3,2);
String sb_copy = new String(sb);
("sb: " + sb );
("sb_sub: " + sb_sub );
("sc: " + sc );
("sc_sub: " + sc_sub );
("sb_copy: " + sb_copy );
The result is:
sb: abcdefghij
sb_sub: de
sc: 0123456789
sc_sub: 34
sb_copy: abcdefghij
2. Method.
illustrate:
1. All methods are public;
2. Writing format: [Modifier] <Return type> <Method name ([Parameter list])>
like:
static int parseInt(String s) means: this method (parseInt) is a class method (static), the return type is (int), and the parameters required by the method are of type String.
1. char charAt(int index): Take a character in the string, and the parameter index refers to the ordinal number in the string. The ordinal number of a string starts from 0 to length()-1.
String s = new String("abcdefghijklmnopqrstuvwxyz");
("(5): " + (5) );
The result is:(5): f
- 2. int compareTo(String anotherString): Comparison of the current String object with anotherString. Equal relationship returns 0; when not equal, the comparison starts from the 0th character of the two strings, and returns the first unequal character difference. In another case, the previous part of the longer string happens to be a shorter string, returning their length difference.
3. int compareTo(Object o): If o is a String object, the function is the same as 2; otherwise, a ClassCastException exception is thrown.
String s1 = new String("abcdefghijklmn");
String s2 = new String("abcdefghij");
String s3 = new String("abcdefghijalmn");
("(s2): " + (s2) );//Return the length difference
("(s3): " + (s3) );//return'k'-'a'The difference
The result is:
(s2): 4
(s3): 10
4. String concat(String str) : Connect the String object with str.
5. boolean contentEquals(StringBuffer sb): Compare the String object with the StringBuffer object sb.
6. static String copyValueOf(char[] data) :
7. static String copyValueOf(char[] data, int offset, int count): These two methods convert the char array into a String, similar to one of the constructors.
8. boolean endsWith(String suffix) : Whether the String object ends in suffix.
String s1 = new String("abcdefghij");
String s2 = new String("ghij");
("(s2): " + (s2) );
The result is:(s2): true
9. boolean equals(Object anObject): Return true when anObject is not empty and is the same as the current String object; otherwise, return false.
10. byte[] getBytes(): Convert the String object to a byte array.
11. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) : This method copies the string into a character array. Among them, srcBegin is the start position of the copy, srcEnd is the end position of the copy, string value dst is the target character array, and dstBegin is the start position of the target character array.
char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!
String s2 = new String("you!");
(0,3,s1,7); //s1=I love you!
( s1 );
The result is:I love you!
- 12. int hashCode() : Returns the hash table code of the current character.
13. int indexOf(int ch) : Find only the first matching character position.
14. int indexOf(int ch, int fromIndex): Start from fromIndex to find the first matching character position.
15. int indexOf(String str): Find only the first matching string position.
16. int indexOf(String str, int fromIndex) : Start from fromIndex to find the first matching string position.
String s = new String("write once, run anywhere!");
String ss = new String("run");
("('r'): " + ('r') );
("('r',2): " + ('r',2) );
("(ss): " + (ss) );
The result is:
('r'): 1
('r',2): 12
(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, the difference is: find the last matching content.
21. int length() : Returns the current string length.
22. String replace(char oldChar, char newChar): Replace the first oldChar in the symbol string with newChar.
23. boolean startsWith(String prefix): Whether the String object starts with prefix.
24. boolean startsWith(String prefix, int toffset): Whether the String object starts from the toffset position, starts with prefix.
String s = new String("write once, run anywhere!");
String ss = new String("write");
String sss = new String("once");
("(ss): " + (ss) );
("(sss,6): " + (sss,6) );
The result is:
(ss): true
(sss,6): true
- 25. String substring(int beginIndex): Take the substring from the beginningIndex position to the end.
26.String substring(int beginIndex, int endIndex) : Take the substring from the beginningIndex position to the endIndex position.
27. char[] toCharArray() : Convert the String object to a char array.
28. String toLowerCase() : Convert string to lowercase.
29. String toUpperCase(): Convert string to uppercase.
String s = new String(" String");
("(): " + () );
("(): " + () );
The result is:
(): STRING
(): 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 into Java character types. These are class methods.