Summary of common methods of 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.Public String ()
A string object that is used to create an empty string without a parameter construction method.
1 string str1 = new string ();
2.Public string (string value)
Creates a string object with the known string value.
1 string str2 = new string ("asdf"); 2 string str3 = new string (STR2);
3.Public String (char[] value)
Creates a string object with the character array value.

1 Char [] value = {"a","b","C","  D"}; 2 New String (value); // 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 New 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 New byte []{]; 2 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 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 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 , // 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.

1String str1 =NewString ("ABC");2String str2 =NewString ("ABC");3 intA = Str1.compareto (STR2);//a>04 intb = Str1.compareto (STR2);//b=05Boolean c = str1.equals (STR2);//C=false6Boolean 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  equals string str =  AA   +"    +"   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.

1String str ="I am a good student";2 intA = Str.indexof ('a');//A = 23 intb = Str.indexof ("Good");//B = 74 intc = Str.indexof ("W",2);//C =-15 intD = Str.lastindexof ("a");//d = 56 intE = Str.lastindexof ("a",3);//e = 2


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 New String ("asDF"); 2 String str1 = Str.tolowercase (); // str1 = "ASDF" 3 String str2 = Str.touppercase (); // str2 = "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.

1String str ="ASDZXCASD";2String str1 = str.replace ('a','g');//str1 = "GSDZXCGSD"3String str2 = str.replace ("ASD","FGH");//str2 = "FGHZXCFGH"4String STR3 = Str.replacefirst ("ASD","FGH");//STR3 = "FGHZXCASD"5String 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 does not handle the intervening space.

1 " "; 2 String str1 = Str.trim (); 3 int a = Str.length (); // A = 6 4 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 = True  3  Boolean b = Str.endwith ( gh  ); // b = True  


3) regionmatches (boolean b, int firststart, String other, int otherstart, int length) // A substring of length is taken from the Firststart position of the current string, 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 a string and returns a Boolean type value.

1 " Student " ; 2 str.contains ("stu"); // true 3 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 ";  

v. Conversion of string to base type
1, string conversion to base type
Java.lang package has byte, Call method for short, Integer, Float, double class:
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) Strong>public static Double parsedouble (String s)
For example:

 1  int  n = integer.parseint ( " 12  "   2  float  f = float.parsefloat ( " 12.34  "   3  double  d = double.parsedouble ( " 1.124  " ); 


2. Basic type conversion to string type
The string ValueOf () method is provided in the string class and is used as the 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:

1 String s1 = string.valueof (n); 2 String s1 = string.valueof (12.34);


3. Binary conversion
Use the methods in the long class to get a variety of binary conversions between integers:
Long.tobinarystring (Long L)
Long.tooctalstring (Long L)
Long.tohexstring (Long L)
Long.tostring (long l, int p)//p as any binary

Summary of common methods of 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.