1. String class
String class In the java.lang package, java uses the String class to create a string variable, and the string variable belongs to the object. Java declares the final class of the String class, and cannot have classes. The String class object cannot be modified after creation. It consists of 0 or more characters and is contained between a pair of double quotes.
2. Creation of String class object <br />String declaration: String stringName;
String creation: stringName = new String(string constant); or stringName = string constant;
3. String class construction method
1. public String()
The parameterless constructor is used to create a String object with an empty string.
String str1 = new String();
2. public String(String value)
Create a String object with a known string value.
String str2 = new String("asdf"); 2 String str3 = new String(str2);
3. public String(char[] value)
Create a String object with a character array value.
char[] value = {"a","b","c","d"};
String str4 = new String(value);//Equivalent to String str4 = new String("abcd");
4. public String(char chars[], int startIndex, int numChars)
Create a String object using numChars characters starting from the startIndex of the character array chars.
char[] value = {"a","b","c","d"};
String str5 = new String(value, 1, 2);//Equivalent to String str5 = new String("bc");
5. public String(byte[] values)
Create a String object with bit array values.
byte[] strb = new byte[]{65,66};
String str6 = new String(strb);//Equivalent to String str6 = new String("AB");
4. Common methods of String class
1. Find the length of the string
public int length()//Return the length of the string
String str = new String("asdfzxc");
int strlength = str.length();//strlength = 7
2. Find a character at a certain position in the string
public char charAt(int index)//Returns the character at the specified position in the string; note that the first character index in the string is 0, and the last one is length()-1.
String str = new String("asdfzxc");
char ch = str.charAt(4);//ch = z
3. Extract substrings
Use the substring method of the String class to extract substrings in a string. This method has two common parameters:
1) public String substring(int beginIndex)//This method starts from the beginningIndex position and takes the remaining characters from the current string as a new string to return.
2) public String substring(int beginIndex, int endIndex)//This method starts from the beginningIndex position and takes out the character at the endIndex-1 position from the current string and returns as a new string.
String str1 = new String("asdfzxc"); String str2 = str1.substring(2);//str2 = "dfzxc" String str3 = str1.substring(2,5);//str3 = "dfz" 4. String comparison
1) public int compareTo(String anotherString)//This method compares the content of the string in dictionary order, and indicates the size relationship between the current string and the parameter string through the returned integer value. If the current object is larger than the parameter, it returns a positive integer, otherwise it returns a negative integer, and equal returns 0.
2) public int compareToIgnore(String anotherString)//Similar to compareTo method, but ignores upper and lower case.
3) public boolean equals(Object anotherObject)//Compare the current string and parameter string, return true when the two strings are equal, otherwise return false.
4) public boolean equalsIgnoreCase(String anotherString)//Similar to the equals method, but ignores upper and lower case.
String str1 = new String("abc");String str2 = new String("ABC");int a = str1.compareTo(str2);//a>0int b = str1.compareTo(str2);//b=0boolean c = str1.equals(str2);//c=falseboolean d = str1.equalsIgnoreCase(str2);//d=true 5. String connection
public String concat(String str)//Connect the string str in the parameter to the back of the current string, the effect is equivalent to "+".
String str = "aa".concat("bb").concat("cc"); is equivalent to String str = "aa"+"bb"+"cc";
6. Search for single characters in strings
1) public int indexOf(int ch/String str)// is used to find characters or substrings in the current string, returning the position where the character or substring appears first from the left in the current string, and returns -1 if it does not appear.
2) public int indexOf(int ch/String str, int fromIndex)//The method is similar to the first one, the difference is that the method is searched backward from the fromIndex position.
3) public int lastIndexOf(int ch/String str)//This method is similar to the first one, the difference is that the method looks forward from the end of the string.
4) public int lastIndexOf(int ch/String str, int fromIndex)//This method is similar to the second method, which is different from this method looking forward from the fromIndex position.
String str = "I am a good student";int a = str.indexOf('a');//a = 2int b = str.indexOf("good");//b = 7int c = str.indexOf("w",2);//c = -1int d = str.lastIndexOf("a");//d = 5int e = str.lastIndexOf("a",3);//e = 2 7. Case conversion of characters in strings
1) public String toLowerCase()//Returns a new string that converts all characters in the current string into lowercase
2) public String toUpperCase()//Returns a new string that converts all characters in the current string into capital
String str = new String("asDF"); String str1 = str.toLowerCase();//str1 = "asdf" String str2 = str.toUpperCase();//str2 = "ASDF" 8. Replacement of characters in strings
1) public String replace(char oldChar, char newChar)//Replace all oldChar characters in the current string with the character newChar and return a new string.
2) public String replaceFirst(String regex, String replacement)//This method replaces the first substring encountered in the current string that matches the string regex, and the new string should be returned.
3) public String replaceAll(String regex, String replacement)//This method replaces all substrings encountered in the current string that matches the string regex, and the new string should be returned.
String str = "asdzxcasd"; String str1 = str.replace('a','g');//str1 = "gsdzxcgsd" String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh" String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd" String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh" 9. Other methods
1)String trim()//Truncate the spaces at both ends of the string, but do not process the spaces in the middle.
String str = " a sd "; String str1 = str.trim(); int a = str.length();//a = 6int b = str1.length();//b = 4
2) boolean statWith(String prefix) or boolean endWith(String suffix)// Used to compare whether the starting character or substring prefix and the terminating character or substring suffix of the current string are the same as the current string. In the overload method, you can also specify the start position of the comparison offset.
String str = "asdfgh"; boolean a = str.statWith("as");//a = true boolean b = str.endWith("gh");//b = true 3) regionMatches(boolean b, int firstStart, String other, int otherStart, int length)// Start comparison from the firstStart position of the current string, take a substring with length length, the other string starts from the otherStart position, specify another string with length length, compare two strings, when b is true, the string is case-insensitive.
4) contains(String str)//Judge whether the parameter s is included in the string and returns a Boolean value.
String str = "student"; str.contains("stu");//true str.contains("ok");//false 5)String[] split(String str)//Decompose str as a separator for string decomposition, and the decomposed character string is returned in the string array.
String str = "asd!qwe|zxc#";
String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";
5. Conversion of strings and basic types
1. Convert strings to basic types
There are calls to Byte, Short, Integer, Float, and Double classes in the java.lang package:
1) public static byte parseByte(String s)
2) public static short parseShort(String s)
3) public static short parseInt(String s)
4) public static long parseLong(String s)
5) public static float parseFloat(String s)
6) public static double parseDouble(String s)
For example:
int n = Integer.parseInt("12");float f = Float.parseFloat("12.34");double d = Double.parseDouble("1.124"); 2. Convert basic types to string types
The String valueOf() method is provided in the String class, which is used as a basic type to convert to a string type.
1)static String valueOf(char data[])
2)static String valueOf(char data[], int offset, int count)
3)static String valueOf(boolean b)
4)static String valueOf(char c)
5)static String valueOf(int i)
6)static String valueOf(long l)
7)static String valueOf(float f)
8)static String valueOf(double d)
For example:
String s1 = String.valueOf(12);
String s1 = String.valueOf(12.34);
3. Category conversion <br /> Use the methods in the Long class to obtain various Category conversion methods:
Long.toBinaryString(long l)
Long.toOctalString(long l)
Long.toHexString(long l)
Long.toString(long l, int p)//p as arbitrary binary
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.