One, String class
String class in the Java.lang package, Java uses the string class to create a string variable that belongs to the object. The final class declared by the Java String class cannot have a class. A string class object cannot be modified after it is created and consists of 0 or more characters, enclosed in a pair of double quotes.
Second, String class object creation
String declaration: String Stringname;
String creation: Stringname = new String (string constant); or Stringname = string constant;
Three, String class construction method
1, Public String ()
parameterless construction method, String object used to create an empty string.
String str1 = new string ();
2, public string (string value)
creates a string object with a known string value.
String str2 = new String ("asdf"); 2 string str3 = new string (STR2);
3, Public String (char[] value)
creates a String object with 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)
creates a string object with NumChars characters starting with 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)
creates a string object with the bit array values.
byte[] strb = new byte[]{65,66};
String STR6 = new string (STRB);//equivalent to String STR6 = new String ("AB");
Iv. common methods of String class
1, to find the length of the string
public int Length ()//returns the length of the string
String str = new String ("Asdfzxc");
int strlength = Str.length ();//strlength = 7
2. To find a character of a position in a 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 is length ()-1.
String str = new String ("Asdfzxc");
char ch = str.charat (4);//ch = Z
3. Extract substring
The substring method of the string class allows you to extract substrings in a string, which has two common parameters:
1 public string substring (int beginindex)//The method returns the remaining characters from the current string as a new string from the Beginindex position.
2 public string substring (int beginindex, int endindex)//The method is returned from the Beginindex position from the current string to the endIndex-1 position character 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)//The method is to compare the string contents in a dictionary order, by returning an integer value that indicates the size of the current string and the argument string. Returns a positive integer if the current object is larger than the argument, or returns a negative integer, equal to 0.
2 the public int comparetoignore (String anotherstring)//is similar to the CompareTo method but ignores case.
3 The public boolean equals (Object anotherobject)//compares the current string and the parameter string, returns True when two strings are equal, or false.
4) Public boolean equalsignorecase (String anotherstring)//is similar to the Equals method but ignores case.
String str1 = new String ("abc");
String str2 = new String ("ABC");
int a = Str1.compareto (str2);//a>0
int b = Str1.compareto (str2);//b=0
Boolean c = str1.equals (STR2);//c= False
Boolean d = str1.equalsignorecase (str2);//d=true
5. String connection
The public string concat (string str)//is connected to the string str in the argument to the back of the current string, and the effect is equivalent to "+".
String str = "AA". Concat ("BB"). Concat ("CC"); Equivalent to string str = "AA" + "BB" + "CC";
6. Single character lookup in string
1 public int indexOf (int ch/string str)//is used to find the character or substring in the current string, to return the first occurrence of a character or substring in the current string from the left, or to return-1 if it is not present.
2 public int indexOf (int ch/string str, int fromindex)/change method is similar to the first one, except that the method looks backward from the fromindex position.
3 public int LastIndexOf (int ch/string str)//The method is similar to the first, except that the method looks forward from the end position of the string.
4 public int LastIndexOf (int ch/string str, int fromindex)//This method is similar to the second method, which distinguishes the method from the Fromindex position to look forward.
String str = "I am a good student";
int a = Str.indexof (' a ');//a = 2
int b = Str.indexof ("good");//b = 7
int c = Str.indexof ("W", 2);//c =-1
int D = Str.lastindexof ("a");//d = 5
int e = Str.lastindexof ("A", 3);//e = 2
7. Uppercase and lowercase conversions of characters in strings
1 public string toLowerCase ()//returns the new string after converting all characters in the current string to lowercase
2 public string toUpperCase ()//Returns a new string that converts all characters in the current string to uppercase
String str = new String ("asdf");
String str1 = Str.tolowercase ();//str1 = "Asdf"
string str2 = Str.touppercase ();//str2 = "ASDF"
8, the replacement of characters in the string
1) public string replace (char Oldchar, char Newchar)//Replaces all Oldchar characters in the current string with a character Newchar and returns a new string.
2 public string Replacefirst (string regex, string replacement)//The method replaces the first string regex encountered in the current string with the contents of the character replacement, The new string should be returned.
3 public string ReplaceAll (string regex, string replacement)//The method replaces all occurrences of the string regex encountered in the current string with the contents of the character replacement. 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" C5/>string STR3 = Str.replacefirst ("ASD", "FGH");//STR3 = "FGHZXCASD"
String STR4 = Str.replaceall ("ASD", "FGH"); STR4 = "FGHZXCFGH"
9, other types of methods
1 String trim ()//truncate the space at both ends of the string, but not for the middle space.
String str = "a SD";
String str1 = Str.trim ();
int a = Str.length ();//a = 6
int b = str1.length ();//b = 4
2) Boolean statwith (String prefix) or boolean endwith (string suffix)// Used to compare the start character of the current string or substring prefix and terminator character or substring suffix is the same as the current string, and you can also specify the start offset of the comparison in the overloaded method.
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 with the Firststart position of the current string. Takes a substring of length, and the other string starts at the Otherstart position, specifying a second string of length, two string comparisons, and a string that is case-insensitive when B is true.
4 contains (string str)//Determines whether the parameter S is contained in the string and returns a Boolean type value.
String str = "Student";
Str.contains ("Stu");//true
str.contains ("OK");//false
5) string[] Split (string str)//to decompose Str as a delimiter, 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 ";
V. Conversion of strings and basic types
1, String conversion to basic type
The Java.lang package has a byte, short, Integer, Float, and double invocation method:
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 ("a");
float f = float.parsefloat ("12.34");
Double d = double.parsedouble ("1.124");
2, basic type conversion to string type
String valueof () is provided in the string class to be used as a base 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, the conversion of the system
Use the methods in the long class to get the various methods of conversion between integers:
Long.tobinarystring (Long L)
Long.tooctalstring (Long L)
Long.tohexstring (Long L)
Long.tostring (long l, int p)//p as arbitrary system
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.