1. Create and initialize a string
String b = "hello";
Create and initialize a string using the constructor method
String();//Initialize string, representing an empty character sequence
String(value);//Create a new object using an existing string constant
String (char[] value);//Create a string using a character array
String(char[] value,int offset,int count);//Intercept the characters from offset to count in the character array to create a non-empty string
String(StringBuffer buffer);//Use StringBuffer object to initialize String object
2. Use of the main methods of the String class:
1. Get the length *.length();//This is different from getting the length in the array, *.length;
2. Compare strings (1) equals() //Determine whether the contents are the same
(2)compareTo() //Judge the size relationship of strings
(3)compareToIgnoreCase(String int) //Ignore the case of letters when comparing
(4)== //Judge whether the content and address are the same
(5)equalsIgnoreCase() // Determine whether the contents are the same while ignoring case
If you want to compare parts of a string to see if they are the same, you can use
(6) regionMatches() //There are two types of public boolean regionMatches (int toffset, String other, int ooffset, int len); indicating that if a substring of the String object is the same character sequence as a substring of the parameter other , then it is true. The string of the String object to be compared starts from index toffset, and the string of other starts from index ooffset, and the length is len.
public boolean reagionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len);//Use Boolean type parameters to indicate whether the comparison of two strings is case-sensitive.
3. Find characters at a certain position in a string
public char charAt(int index);//Returns the character at the specified index index position, the index range starts from 0
4. Find the position where the first or last word of the specified string appears in the string
The String class provides two methods for finding the first occurrence of a string at a specified position.
(1)public int indexOf(String str);//Retrieve str from the beginning of the string and return the position of the first occurrence. If it does not appear, return -1
(2)public int indexOf(String str,int fromIndex);//Retrieve str starting from the fromIndex character of the string
There are two ways to find the last occurrence of
(1)public int lastIndexOf(String str);
(2)public int lastIndexOf(String str,int fromIndex);
If you don't care about the exact position of the string, you can use public boolean contains(CharSequence s);
5. Check the starting character and ending character of the string
Two ways to start a string
(1)public boolean starWith(String prefix,int toffset);//If the string sequence represented by the parameter prefix is a substring of the object starting from the index toffset, then return true
(2)public boolean starWith(String prefix);
end string method
public boolean endsWith(String suffix);
6. Intercept substring
(1)public String subString(int beginIndex);
(2) public String subString(int beginIndex, int endIndex);//The returned string is a string starting from beginIndex to endIndex-1
To return the last 4 digits, you can write Syetem.out.println(*.subString()(*.length()-4));
7. String replacement
Two methods
(1)public String replace(char oldChar,char newChar);
(2)public String replace(CharSequence target,CharSequence replacement);//Replace the original etarget subsequence with the replacement sequence and return the new string
(3)public String replaceAll(String regex,String replacement);//Use regular expressions to match strings
8. String case conversion
(1)public String toLowerCase(Locale locale);
(2)public String toLowerCase();
(3)public String toupperCase(Locale locale);
(4)public String toUpperCase();
9. Remove leading and trailing spaces from strings
*.trim();
10. String conversion
1. Convert a string into a character array
public char[] toCharArray();
2. Convert the string into a string array
public String[] split(String regex);//regex is the given match
3. Convert other data types into strings
(1)public static String valueOf(boolean b);
(2)public static String valueOf(char c);
(3)public static String valueOf(int i);
(4)public static String valueOf(long i);
(5)public static String valueOf(float f);
(6)public static String valueOf(double d);
(7)public static String valueOf(char[] data);
(8)public static String valueOf(Object obj);
Creation and initialization of mutable strings
Two methods:
public StringBuffer();
public StringBuffer(int caoacity);
Use of the main methods of the StringBuffer class:
1. Get variable string length
(1)public int length();
(2)public int capacity();
(3)public void setLength(int newLength);
2. Compare variable strings
Use the equals() method of the String class to compare, but differ.
The equals() method in class Object compares whether the addresses of two objects are equal, not just the contents. However, when the String class inherits the Object class, it overrides the equals() method and only compares the contents of the two objects. Are they equal?
In the StringBuffer class, the equals() method of the Object class is not overridden, so the address and content are compared.
3. Append and insert strings
(1)Append public StringBuffer append(type t);
(2) Insert public StringBuffer insert(int offset,type t);//Add a string of type type at offset
4. Reverse and delete strings
(1)Reverse public StringBuffer reverse();
(2)Delete public StringBuffer delete(int start,int end);
5. Reduce storage space used for variable character sequences
public void trimToSize();
6. Convert StringBuffer class to String class
public String toString();