Common methods of the Java string class are described in detail

  • 2020-04-01 01:09:27
  • OfStack

String: String type
One, the constructor
 
String(byte[ ] bytes) Through: byte Arrays construct string objects.  
String(char[ ] value) Through: char Arrays construct string objects.  
String(Sting original) : construct a original A copy of. Copy one original .  
String(StringBuffer buffer) Through: StringBuffer Arrays construct string objects.  

Such as:
 
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: sb:abcdefghij
Sb_sub: DE
Sc: 0123456789
Sc_sub: 34
Sb_copy: abcdefghij
Second, the method :
. All methods are public.
, writing format: [modifier] < Return type > < Method name ([parameter list])>
For example: static int parseInt(String s)
Represents this method (parseInt) as a class method (static), returns the type as (int), and the method needs to be of type String.
Char charAt(int index) : takes a character in a string, where the parameter index refers to the ordinal number in the string. The ordinal of a string starts from 0 to length()-1.
For example: String s = new String (" abcdefghijklmnopqrstuvwxyz ");
System.out.println(" s.harat (5): "+ s.harat (5));
The results are: s.harat (5): f
Int compareTo(String anotherString) : the current String object is compared with anotherString. Equality returns 0; In the case of inequality, the comparison starts at the 0th character of the two strings and returns the difference between the first unequal character. In the other case, the preceding part of the longer string happens to be the shorter string and returns the difference in their length.
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)); // returns length difference
System.out.println("s1.compareTo(s3): "+ s1.compareTo(s3)); // returns the difference between 'k'-'a'
The result is: s1.compareTo(s2): 4
S1.com pareTo (s3) : 10
String concat(String STR) : concatenates 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) :
Static String copyValueOf(char[] data, int offset, int count) : these two methods convert the char array to a String, similar to one of the constructors.
Boolean endsWith(String suffix) : whether the String object ends in suffix.
For example: String s1 = new String("abcdefghij");
String s2 = new String("ghij");
Println ("s1. EndsWith (s2): "+ s1. EndsWith (s2));
The result is: s1. EndsWith (s2): true
Boolean equals(Object anObject) : returns true when anObject is not null and is the same as the current String Object; Otherwise, false is returned.
Byte [] getBytes() : converts the String object to a byte array.
Void getChars(int srcBegin, int srcEnd, char[] DST, int dstBegin) : this method copies the string into an array of characters. Where, srcBegin is the starting position of the copy, srcEnd is the ending position of the copy, the string value DST is the target character array, and dstBegin is the starting position of the copy of the target character array.
For example: the char [] s1 = {' I ', ' ', 'l', 'o', 'v', 'e', ', 'h', 'e', 'r', '! '}; / / s1 = I love its ehrs!
String s2 = new String("you!") ); S2. GetChars (0, 3, s1, 7); / / s1 = I love you!
System. The out. Println (s1);
The result: I love you!
12. Int hashCode() : returns the hashCode of the current character.
Int indexOf(int ch) : find only the first matching character position.
Int indexOf(int ch, int fromIndex) : find the first matching character position from fromIndex.
Int indexOf(String STR) : only find the first matching String position.
Int indexOf(String STR, int fromIndex) : find the first matching String position from fromIndex.
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(" I 'ndexof (ss): "+ I 'ndexof (ss));
The result is: s I ndexOf('r'): 1
S.i ndexOf (' r ', 2) : 12
S.i ndexOf (ss) : 12
17. Int lastIndexOf (int ch)
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, 16, except: find the last matched content.
Public class CompareToDemo {
Public static void main (String[] args) {
String s1 = new String("acbdebfg");
System. The out. Println (s1. LastIndexOf ((int) 'b', 7));
}
}
Results: 5
(where the fromIndex parameter is 7, the number of digits preceding the last character g of the string acbdebfg. Both start matching the character c and find the last position that matches b. So it's 5.)
21. Int length() : returns the current string length.
22. String replace(char oldChar, char newChar) : replaces the first oldChar in the character String with newChar.
23. Boolean startsWith(String prefix) : whether the String object startsWith a prefix.
Boolean startsWith(String prefix, int toffset) : this String object counts from the toffset position and whether it startsWith a 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.tartswith (ss): "+ s.tartswith (ss));
Println (" s.tartswith (SSS,6): "+ s.tartswith (SSS,6));
The result is: s.tartswith (ss): true
S.s tartsWith SSS, (6) : true
25. String substring(int beginIndex) : takes the substring from the beginIndex position to the end.
26.String substring(int beginIndex, int endIndex) : takes a substring from the beginIndex position to the endIndex position.
Char [] toCharArray() : converts this String object to a char array.
28. String toLowerCase() : converts a String toLowerCase.
29. String toUpperCase() : converts a String toUpperCase.
For example: String s = new String("java.lang.Class String");
Println (" s.ouppercase (): "+ s.ouppercase ());
System.out.println(" s.owl case (): "+ s.owl case ());
The result: s.ouppercase (): java.lang.class STRING
S.t oLowerCase () : Java. Lang. The class string
30. Static String valueOf(Boolean b)
31. Static String valueOf(char c)
32. Static String valueOf(char[] data)
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 to 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 a string;
Public int indexOf(String STR,int fromIndex)
Returns the position of the string where STR first appeared since fromIndex;
Public Boolean equalsIgnoreCase (String another)
Compare whether the string is the same as another (ignoring case);
Public String replace(char oldchar,char newChar)
Replace the oldChar character with the newChar character in the string
Public Boolean startsWith (String prefix)
Determines whether the string begins with a prefix string;
Public Boolean endsWith (String suffix)
Determines whether a string ends with a suffix string;
Public String toUpperCase ()
Returns a string in uppercase for that string;
Public String toLowerCase ()
Returns a string in lowercase for that string
Public String substring (int beginIndex)
Returns the substring of the string beginning at beginIndex and ending at beginIndex.
Public String substring(int beginIndex,int endIndex)
Returns the substring of the string that begins at beginIndex and ends at endsIndex
Public String trim ()
Returns the string after the opening and closing Spaces are removed from the string
Public String [] the split (String regex)
Separates a string by the specified delimiter and returns the delimited array of strings
Example:
 
public class SplitDemo{ 
public static void main (String[] args) { 
String date = "2008/09/10"; 
String[ ] dateAfterSplit= new String[3]; 
dateAfterSplit=date.split("/"); //Split the date string with "/" as the delimiter and put the result into three strings.
for(int i=0;i<dateAfterSplit.length;i++) 
System.out.print(dateAfterSplit[i]+" "); 
} 
} 

Run result: 2008/09 10 // result: 3 split strings
Example:
TestString1. Java:
The program code
 
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 The programmer " ; 
String sr = s.replace(' I ',' you ') ; 
System.out.println(sr) ; 
} 
} 

TestString2. Java:
The program code
 
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) ; 
} 

Related articles: