Java String class

Source: Internet
Author: User
Tags first string

First, the String class
String class in the Java.lang package, Java uses the string class to create a string variable that is part of the object. Java does not have a class for the final class that declares the string class. A string class object cannot be modified after it is created, consisting of 0 or more characters, enclosed by a pair of double quotation marks.
Second, the creation of the String class object
String declaration: String Stringname;
String creation: Stringname = new String (string constant), or stringname = string constant;
Third, the method of constructing string class
1. PublicString ()
A string object that is used to create an empty string without a parameter construction method.
1 String str1 = new string ();
2, Publicstring (string value)
Creates a string object with the known string value.
1 String str2 = new String ("asdf"); 2 String str3 = new string (STR2);
3. PublicString (char[] value)
Creates a string object with the character array value.

1 Char[] value = {"A", "B", "C", "D"};2 string str4 = new string (value);//is equivalent to string STR4 = new String ("ABCD");


4, PublicString (char chars[], int startIndex, int numChars)
Creates a string object with NumChars characters starting with the startindex of the character array chars.

1 Char[] value = {"A", "B", "C", "D"};2 string str5 = new String (value, 1, 2);//equivalent to String STR5 = new string ("BC");


5. PublicString (byte[] values)
Creates a string object with a bit array of values.

1 byte[] strb = new Byte[]{65,66};2 String str6 = new string (STRB);//equivalent to String STR6 = new String ("AB");

Iv. common methods of String class
1. Find string length
public int Long ()//Returns the length of the string

1 String str = new String ("Asdfzxc"), 2 int strlength = Str.length ();//strlength = 7


2. To find a character string in a position
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.

1 String str = new String ("Asdfzxc"); 2 char ch = str.charat (4);//ch = Z


3. Extract the Sub-string
You can extract substrings in a string using the substring method of the string class, which has two common parameters:
1) Publicstring substring (int beginindex)//This method takes the remaining characters from the current string to be returned as a new string from the Beginindex position.
2) PublicString substring (int beginindex, int endIndex)//This method starts from the Beginindex position, The character that is removed from the current string to the endIndex-1 position is returned as a new string.

1 String str1 = new String ("Asdfzxc"), 2 string str2 = Str1.substring (2),//str2 = "Dfzxc" 3 string str3 = str1.substring (2,5 );//STR3 = "DFZ"


4. String comparison
1) Publicint CompareTo (string anotherstring)//This method is a dictionary-size comparison of string contents, indicating the size of the current string with the parameter string by returning an integer value. Returns a positive integer if the current object is larger than the argument, and returns a negative integer, equal to 0.
2) Publicint Comparetoignore (String anotherstring)//is similar to the CompareTo method, but ignores case.
3) PublicBoolean equals (Object anotherobject)//compares the current string and argument string, returns True when two strings are equal, otherwise returns false.
4) Publicboolean equalsignorecase (String anotherstring)//is similar to the Equals method but ignores case.

1 String str1 = new String ("abc"), 2 string str2 = new String ("abc"), 3 int a = Str1.compareto (str2),//a>04 int b = str1. CompareTo (STR2);//b=05 boolean c = str1.equals (STR2);//c=false6 Boolean d = str1.equalsignorecase (str2);//d=true


5. String connection
public string concat (String str)//connects the string str in the argument to the back of the current string, and the effect is equivalent to "+".

1 string str = "AA". Concat ("BB"). Concat ("CC"), 2 equivalent to String str = "AA" + "BB" + "CC";


6. Single character lookup in string
1) Publicint indexOf (int ch/string str)///used to find the character or substring in the current string, returns the first occurrence of a character or substring in the current string from the left, or 1 if it does not appear.
2) Publicint indexOf (int ch/string str, int fromIndex)//The method is similar to the first one, except that the method looks backwards from the fromIndex position.
3) Publicint lastIndexOf (int ch/string str)//This method is similar to the first, except that the method looks forward from the end of the string.
4) Publicint lastIndexOf (int ch/string str, int fromIndex)//This method is similar to the second method, except that the method looks forward from the FromIndex position.

1 String str = "I am a good student", 2 int a = Str.indexof (' A '),//a = int b = Str.indexof ("good");//b = "int c = str". IndexOf ("W", 2);//c = -15 int d = str.lastindexof ("a"),//d = 2 ("A", 3), Str.lastindexof


7. Case conversion of characters in string
1) Publicstring toLowerCase ()//Returns a new string after converting all characters in the current string to lowercase
2) Publicstring toUpperCase ()//Returns a new string that converts all characters in the current string to uppercase

1 String str = new String ("AsDF"); 2 string str1 = Str.tolowercase ();//str1 = "AsDF" 3 string str2 = Str.touppercase ();//str 2 = "ASDF"


8. Substitution of characters in a string
1) publicstring replace (char OldChar, char Newchar)//Replaces all OldChar characters in the current string with the character Newchar, and returns a new string.
2) Publicstring Replacefirst (string regex, string replacement)// The method substitutes the contents of the character replacement for the first string encountered in the current string and matches the substring of the Regex match, and the new string should be returned.
3) Publicstring ReplaceAll (string regex, string replacement)// The method replaces any substring encountered in the current string with the string regex matched by the contents of the character replacement, and the new string should be returned.

1 string str = "ASDZXCASD"; 2 string str1 = Str.replace (' A ', ' G ');//str1 = "Gsdzxcgsd" 3 String str2 = str.replace ("ASD", "FGH //STR2 = "FGHZXCFGH" 4 string str3 = Str.replacefirst ("ASD", "FGH");//STR3 = "FGHZXCASD" 5 String STR4 = Str.replaceall ("A SD "," FGH ");//STR4 =" Fghzxcfgh "


9. Other types of methods
1)string trim ()//Truncate the space at both ends of the string, but does not handle the intervening space.

1 string str = "a SD"; 2 string str1 = Str.trim (); 3 int a = Str.length ();//a = int b = str1.length ();//b = 4


2)boolean statwith (string prefix) or boolean endwith (string suffix)// To compare the starting character or substring of the current string prefix and the terminating character or substring suffix is the same as the current string, the overloaded method can also specify the starting position of the comparison offset.

1 String str = "ASDFGH"; 2 Boolean a = Str.statwith ("as"),//a = True3 Boolean b = Str.endwith ("gh");//b = True


3)Regionmatches (boolean b, int firststart, string other, int otherstart, int length)//starting from the Firststart position of the current string, Take a substring of length, and the other string starts at the Otherstart position, specifying another string of length, two string comparisons, and when B is true, the string is not case-sensitive.
4)contains (string str)//Determines whether the parameter S is contained in the string and returns a Boolean type value.

1 String str = "Student"; 2 str.contains ("Stu");//true3 str.contains ("OK");//false


5)string[] Split (string str)//string decomposition of Str as a delimiter, the decomposed character string is returned in an array of strings.

1 String str = "asd!qwe|zxc#"; 2 string[] str1 = Str.split ("!| # ");//str1[0] =" ASD "; str1[1] =" qwe "; str1[2] =" ZXC ";

l, int p)//p as any binary

Java String class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.