String: string type
1. Constructor
Copy the code code as follows:
String(byte[ ] bytes): Construct a string object from a byte array.
String(char[ ] value): Construct a string object from a char array.
String(Sting original): Construct a copy of original. That is: copy an original.
String(StringBuffer buffer): Construct a string object through the StringBuffer array.
For example:
Copy the code code as follows:
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); //abcdefghij
String sb_sub = new String(b,3,2); //de
String sc = new String(c); //0123456789
String sc_sub = new String(c,3,2); //34
String sb_copy = new String(sb); //abcdefghij
System.out.println("sb:"+sb);
System.out.println("sb_sub:"+sb_sub);
System.out.println("sc:"+sc);
System.out.println("sc_sub:"+sc_sub);
System.out.println("sb_copy:"+sb_copy);
Output result: sb:abcdefghij
sb_sub:de
sc:0123456789
sc_sub:34
sb_copy:abcdefghij
2. Method :
Note: ①. All methods are public.
②. Writing format: [Modifier] <return type><method name ([parameter list])>
For example: static int parseInt(String s)
Indicates that this method (parseInt) is a class method (static), the return type is (int), and the method requires a String type.
1. char charAt(int index): Get a certain character in the string, where the parameter index refers to the ordinal number in the string. The ordinal number of the string starts from 0 and goes to length()-1.
For example: String s = new String("abcdefghijklmnopqrstuvwxyz");
System.out.println("s.charAt(5): " + s.charAt(5) );
The result is: s.charAt(5): f
2. int compareTo(String anotherString): Compare the current String object with anotherString. The equality relationship returns 0; when they are not equal, the comparison starts from the 0th character of the two strings and returns the first unequal character difference. In another case, the front part of the longer string happens to be the shorter string. , return their length difference.
3. int compareTo(Object o): If o is a String object, the function is the same 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 result of 'k'-'a': s1.compareTo(s2): 4
s1.compareTo(s3): 10
4. String concat(String str): Concatenate 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 String, similar to one of the constructors.
8. boolean endsWith(String suffix): Whether the String object ends with suffix.
For example: String s1 = new String("abcdefghij");
String s2 = new String("ghij");
System.out.println("s1.endsWith(s2): " + s1.endsWith(s2) );
The result is: s1.endsWith(s2): true
9. boolean equals(Object anObject): When anObject is not empty and is the same as the current String object, return true; otherwise, return false.
10. byte[] getBytes(): Convert the String object into a byte array.
11. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin): This method copies the string to the character array. Among them, srcBegin is the starting position of the copy, srcEnd is the end position of the copy, the string value dst is the target character array, and dstBegin is the copy starting position of the target character array.
For example: 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 );
The result is: I love you!
12. int hashCode(): Returns the hash table code of the current character.
13. int indexOf(int ch): Only find the first matching character position.
14. int indexOf(int ch, int fromIndex): Find the first matching character position starting from fromIndex.
15. int indexOf(String str): Only find the first matching string position.
16. int indexOf(String str, int fromIndex): Find the first matching string position starting from fromIndex.
For example: String s = new String("write once, run anywhere!");
String ss = new String("run");
System.out.println("s.indexOf('r'): " + s.indexOf('r') );
System.out.println("s.indexOf('r',2): " + s.indexOf('r',2) );
System.out.println("s.indexOf(ss): " + s.indexOf(ss) );
The result is: s.indexOf('r'): 1
s.indexOf('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. The difference is: 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
(The parameter of fromIndex is 7, which is the number of digits counting forward from the last character g of the string acbdebfg. Since it starts matching from character c, it looks for the last position that matches b. So the result is 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 object starts with prefix.
24. boolean startsWith(String prefix, int toffset): Whether the String object starts with prefix, counting from the toffset position.
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) );
The result is: s.startsWith(ss): true
s.startsWith(sss,6): true
25. String substring(int beginIndex): Get the substring starting from the beginIndex position to the end.
26.String substring(int beginIndex, int endIndex): Take the substring starting from the beginIndex position to the endIndex position.
27. char[ ] toCharArray(): Convert the String object into a char array.
28. String toLowerCase(): Convert the string to lowercase.
29. String toUpperCase(): Convert the 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.toUpperCase(): 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 methods are used to convert various types into Java character types. These are class methods.
Common methods of String class in Java:
public char charAt(int index)
Returns the index character in the string;
public int length()
Returns the length of the string;
public int indexOf(String str)
Returns the position of the first occurrence of str in the string;
public int indexOf(String str,int fromIndex)
Returns the position of the first occurrence of str in the string starting from fromIndex;
public boolean equalsIgnoreCase(String another)
Compares whether the string is the same as another (ignoring case);
public String replace(char oldchar,char newChar)
Replace oldChar characters with newChar characters in a string
public boolean startsWith(String prefix)
Determine whether the string begins with the prefix string;
public boolean endsWith(String suffix)
Determine whether a string ends with a suffix string;
public String toUpperCase()
Returns a string that is the uppercase version of the string;
public String toLowerCase()
Returns a string that is the lowercase version of the string
public String substring(int beginIndex)
Returns the substring from beginIndex to the end of the string;
public String substring(int beginIndex,int endIndex)
Returns the substring of the string starting from beginIndex to the end of endsIndex
public String trim()
Returns the string after removing leading and trailing spaces.
public String[] split(String regex)
Separate a string according to the specified delimiter and return the separated string array instance:
Copy the code code as follows:
public class SplitDemo{
public static void main (String[] args) {
String date = "2008/09/10";
String[ ] dateAfterSplit= new String[3];
dateAfterSplit=date.split("/"); //Use "/" as the separator to split the date string and put the results into three strings.
for(int i=0;i<dateAfterSplit.length;i++)
System.out.print(dateAfterSplit[i]+" ");
}
}
Running result: 2008 09 10 //The result is 3 split string instances:
TestString1.java:
program code
Copy the code code as follows:
public class TestString1
{
public static void main(String args[]) {
String s1 = "Hello World";
String s2 = "hello world";
System.out.println(s1.charAt(1));
System.out.println(s2.length());
System.out.println(s1.indexOf("World"));
System.out.println(s2.indexOf("World"));
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
String s = "I am a J2EE programmer";
String sr = s.replace('I','you');
System.out.println(sr);
}
}
TestString2.java:
program code
Copy the code code as follows:
public class TestString2
{
public static void main(String args[]) {
String s = "Welcome to Java World!";
String s2 = " magci " ;
System.out.println(s.startsWith("Welcome"));
System.out.println(s.endsWith("World"));
String sL = s.toLowerCase();
String sU = s.toUpperCase();
System.out.println(sL);
System.out.println(sU);
String subS = s.substring(11);
System.out.println(subS);
String s1NoSp = s2.trim();
System.out.println(s1NoSp);
}