[Java] common string methods in development, java strings
Java string is very powerful, and every method of it is also very useful.
Java strings are commonly used in two types of String classes: String class and StringBuffer class.
Sting class
The object of the String class is unchangeable.
Create String
String () String (String str) String (char value []) // use a character array to generate a String object String (char value [], int offset, int count) // create a String object with count characters starting with the offset position of the character array value
Common Methods
Int length () String toLowerCase () // returns the lowercase String toUpperCase () of the current String // returns the uppercase char of the current String [] toCharArray () // return the String array of the current String trim () // Delete the leading and trailing spaces of the current String and return the New String
Instance:
String str = new String ("Hello world"); str. length (); // The return str length is 11str. toLowerCase (); // converts str to lowercase, "hello world" str. toUpperCase (); // converts str to uppercase, "hello world" char [] strChar = str. toCharArray (); // converts str to the strChar character array str. trim (); // Delete the leading and trailing spaces, "Hello world"
Comparative Method
Boolean regionMatches (int toffset, String str, int ooffset, int len) // compare the len characters starting from the toffset of the String with those starting from ooffset, returns true if they are consistent (this can be used to check the number of times a character String appears in the current String) boolean regionMatches (boolean IgnoreCase, int toffset, String str, int ooffset, int len) // same as above, IgnoreCase determines whether to ignore case sensitivity. If IgnoreCase is set to true, Case sensitivity String concat (String str) is ignored) // returns the Unicode of the new int compareTo (String str) String connected to the str String at the same position. If the two strings are equal, 0 is returned, otherwise, the current String is greater than str and returns the difference int compareToIgnoreCase (String str) // ignore case-insensitive comparison, same as boolean equals (Object anObj) // compare the values of the two objects to be equal. here, we will compare whether two String objects are equal boolean inclusignorecase (String anotherString) // ignore case sensitivity, and compare whether the two String objects are equal boolean startsWith (String prefix [, int toffset]) // determine whether the current string overhead from toffset starts with the prefix parameter. [] brackets indicate that boolean endsWidth (string prefix [, int toffset] can be omitted) // determine whether the current string starts from toffset and ends with the prefix Parameter
Search Method
// Search for the character ch. Note that the character int indexOf (int ch) // locate the first character ch from the front to the back. If no value is found,-1int indexOf (int ch, int fromIndex) // locate the location where the first character ch appears from the position of fromIndex. If-1int lastIndexOf (int ch) is not found) // locate the location where the first ch character appears from the back and forward. If-1int lastIndexOf (int ch, int fromIndex) is not found, the return value is-1int lastIndexOf (int ch, int fromIndex) // locate the location where the first character ch appears before and after fromIndex.-1 is not returned. // search for int indexOf (String str) on the substring str) // search for the first occurrence position of the substring str from the beginning and back of the current String.-1int indexOf (String str, int fromIndex) is not returned) // search for the first occurrence position of the substring str from the position of the current String fromIndex.-1int lastIndexOf (String str) is not returned) // locate the first occurrence position of the substring str from the end of the current String. If-1int lastIndexOf (String str, int fromIndex) is not found, the return value is-1int lastIndexOf (String str, int fromIndex) // search for the first occurrence position of the substring str from the position of the current string fromIndex.-1 char charAt (int index) is not found. // return the character at the position of the index of the current string
Replacement Method
// Replace String replace (char oldchar, char newchar) // replace all oldcha character r in the String with newchar character String replaceFirst (String regex, String replacement) // Replace the first substring in the String that matches the regular expression regex with the new String replacement with String replaceAll (String regex, String replacement) // replace all substrings in the String that match the regular expression regex with the new String replacement with the String substring (int start [, int end]) // returns the substring from start to end-1. If end is omitted, the substring is from start to end. string [] split (String regex) // returns the String array separated by the current String using a regular expression
Other methods
Convert a digital string to a basic type
public static byte parseByte(String s) throws NumberFormatExceptionpublic static short parseShort(String s) throws NumberFormatExceptionpublic static short parseInt(String s) throws NumberFormatExceptionpublic static long parseLong(String s) throws NumberFormatExceptionpublic static float parseFloat(String s) throws NumberFormatExceptionpublic static double parseDouble(String s) throws NumberFormatException
Example:
int a = Integer.parseInt(“23”);
Convert other types to strings
Public static String valueOf (int n) public static String valueOf (char [] data) public static String valueOf (Object obj) public static String copyValueOf (char [] data) equivalent to valueOf (char [] data)
Example:
String.valueOf(334);
StringBuffer class
StringBuffer () // create an empty StringBuffer object StringBuffer (int length) // create a StringBuffer object StringBuffer (String str) with length) // create a str string StringBuffer Object StringBuffer append (Object obj) // Add the Object obj to the StringBuffer Object StringBuffer insert (int position, Object obj) // Insert the object obj to the position StringBuffer setCharAt (int position, char ch) in the StringBuffer object // Replace the position StringBuffer deleteCharAt (int position) in the StringBuffer object with the character ch) // Delete the character StringBuffer replace (int start, int end, String str) at the position. // replace the start to end-1 position in the StringBuffer object with the String str