The String class is located under the Java.lang package, the String class represents strings, strings are constants, their values cannot be changed after they are created, and in order to support mutable strings, Java also provides us with StringBuilder and StringBuffer classes.
1. Common methods in the String class
A. Methods related to indexes and lengths
return value type |
Name of function |
Function |
Char |
charAt (int index) |
Returns the value at the specified index char . |
String |
subString (int beginindex,int endIndex) |
Returns a new string that is a substring of this string. The substring at which beginIndex to start, until endIndex - 1 the character at the index. |
Int |
indexOf (int ch) |
Returns the index at which the specified character first appears in this string. |
Int |
IndexOf (String str) |
Returns the index at which the specified substring first appears in this string. |
Int |
lastIndexOf (int ch) |
Returns the index of the specified character at the last occurrence in this string. |
Int |
LastIndexOf (String str) |
Returns the index of the rightmost occurrence of the specified substring in this string. |
Int |
Length () |
Returns the length of this string. |
Boolean |
IsEmpty () |
Returns True when and only if length() 0 . |
Note : When you execute the substring method, a new character array is created instead of using the existing character array.
B. Methods related to comparison, inclusion, and conversion
return value type |
Name of function |
Function |
String |
Concat (String str) |
Connects the specified string to the end of this string. |
Boolean |
Contains (charsequence s) |
Returns True when and only if this string contains a sequence of specified char values. |
Boolean |
Endwith (String suffix) |
Tests whether this string ends with the specified suffix. |
Boolean |
StartsWith (String prefix) |
Tests whether this string starts with the specified prefix. |
Int |
Comparetoignorecase (String str) |
Compares two strings in a dictionary order, regardless of case. |
Boolean |
Equalsignorecase (String anotherstring) |
Compare this to String another String , regardless of case. |
Byte[] |
GetBytes (CharSet CharSet) |
String encodes this into a byte sequence with the given charset and stores the result in a new byte array. |
Char[] |
ToCharArray () |
Converts this string to a new character array. |
String |
toUpperCase () |
Use the default locale's rules to String convert all characters in this to uppercase. |
String |
toLowerCase () |
Use the default locale's rules to String convert all characters in this to lowercase. |
C. Methods related to regular expressions
return value type
function name |
function |
string |
matches (string regex) |
|
string |
replaceall (string regex,string replacement) |
replaces this string with the given replacement for all occurrences of a given regular expression Character string. |
string |
replacefirst (String regex,string replacement) |
replaces this string with the given replacement to match the first substring of a given regular expression. |
string[] |
split (String regex) |
splits this according to the match of the given regular expression String. |
String |
trim () |
Returns a copy of the string, ignoring leading and trailing blanks. |
String |
replace (char Oldchar,char newchar) |
return A new string that is obtained by replacing all OldChar occurrences in this string with Newchar . |
1 Public Static voidMain (string[] args) {2 //remove leading and trailing blanks from a string3String str = "ABC ed F";4 System.out.println (str);5 System.out.println (Str.trim ());6 //separates a string with whitespace characters7string[] Strarray = Str.split ("\\s+");8 System.out.println (strarray.length);9 for(String s:strarray) {Ten System.out.println (s); One } A}
Operation Result:
2. Off-Topic
A. Once the string object is created immutable, and StringBuilder and StringBuffer are mutable, this means that they can still modify their values after they are created, StringBuffer is synchronized, it is thread-safe, However, it is slower than StringBuilder and it is recommended to use StringBuilder if there is no safety requirement.
B. The connection of strings in Java is implemented with "+", which is done through the StringBuilder or StringBuffer classes and their append methods.
C. Converting a string to a date
1 String str = "Fri 17,2013"; 2 New SimpleDateFormat ("MMM d,yy", Locale.english). Parse (str); 3 System.out.println (date);
Java-string Class Learning Notes