Methods and descriptions of the string class in Java

Source: Internet
Author: User

String: string type

I,Constructor
String (Byte [] Bytes):Constructing string objects using Byte Arrays.
String (Char [] Value):Construct a String object using a char array.
String (Sting Original): ConstructOriginalOfCopy. That is:Copy an original.
String (Stringbuffer Buffer): Constructs a String object through the stringbuffer array.
For example:
Byte [] B = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'h ', 'I', 'J '};
Char [] C = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9 '};
String sb = new string (B );//Abcdefghij
String sb_sub = new string (B, 3, 2 );//De
String SC = new string (c );//0123456789
String SC _sub = new string (C, 3, 2 );//34
String sb_copy = new string (SB );//Abcdefghij
System. Out. println ("SB:" + Sb );
System. Out. println ("sb_sub:" + sb_sub );
System. Out. println ("SC:" + SC );
System. Out. println ("SC _sub:" + SC _sub );
System. Out. println ("sb_copy:" + sb_copy );
Output result:SB: abcdefghij
Sb_sub: de
SC: 0123456789
SC _sub: 34
Sb_copy: abcdefghij

Ii. Method:

Note: ①. All methods are public.
②. Writing format: [modifier] <return type> <method name ([parameter list])>

Example: static int parseint (string S)
It indicates that this method (parseint) is a class method (static), the return type is (INT), and the method must be of the string type.

1.Char Charat (INT index):Take a character from a stringThe index parameter indicates the ordinal number in the string. The ordinal number of a string starts from 0 to length ()-1.
Example: String S = new string ("abcdefghijklmnopqrstuvwxyz ");
System. Out. println ("S. charat (5):" + S. charat (5 ));
Result: S. charat (5): F
2.Int compareto (string anotherstring):Comparison between the current String object and anotherstring.EqualLinkReturns 0.;Not equalWhen comparing two strings with 0th characters,Returns the first unequal character difference., In another case,The front part of a long string happens to be a short string and Returns their length difference.
3.Int compareto (Object O): If O is a string object, it has the same function as 2; otherwise, it throwsClasscastexceptionException.
Example: String S1 = new string ("abcdefghijklmn ");
String S2 = new string ("abcdefghij ");
String S3 = new string ("abcdefghijalmn ");
System. Out. println ("s1.compareto (S2):" + s1.compareto (S2); // return the length difference
System. Out. println ("s1.compareto (S3):" + s1.compareto (S3); // return the difference between 'K' and 'A '.
Result: s1.compareto (S2): 4
S1.compareto (S3): 10
4.String Concat (string Str):Concatenates the string object with Str.
5.Boolean contentequals (stringbuffer SB): Compares the string object with the stringbuffer object sb.
6.Static string copyvalueof (char [] data):
7.Static string copyvalueof (char [] data, int offset, int count): These two methodsConvert a char array to a string, Similar to a constructor.
8.Boolean endswith (string suffix):Whether the string object ends with suffix.
Example: String S1 = new string ("abcdefghij ");
String S2 = new string ("ghij ");
System. Out. println ("s1.endswith (S2):" + s1.endswith (S2 ));
Result: s1.endswith (S2): True
9.Boolean equals (Object anobject):Returns true if anobject is not empty and is the same as the current String object. Otherwise, returns false..
10.Byte [] getbytes ():Converts a String object to a byte array..
11.Void getchars (INT srcbegin, int srcend, char [] DST, int dstbegin):This method copies the string to the character array.. Srcbegin indicates the start position of the copy, srcend indicates the end position of the copy, DST indicates the destination character array, and dstbegin indicates the start position of the copy of the target character array.
For example, char [] S1 = {'I', '', 'l', 'O', 'V', 'E','', 'h ', 'E', 'R ','! '}; // S1 = I love her!
String S2 = new string ("you! "); S2.getchars (0, 3, S1, 7); // S1 = I love you!
System. Out. println (S1 );
The result is: I love you!
12.Int hashcode ():Returns the hash table code of the current character..
13.Int indexof (INT ch):Only find the First Matching character location.
14.Int indexof (int ch, int fromindex):Find the first matched character location from fromindex.
15.Int indexof (string Str):Only find the first position matching the string.
16.Int indexof (string STR, int fromindex):Find the first matched string position from fromindex.
Example: String S = new string ("write once, run anywhere! ");
String Ss = new string ("Run ");
System. Out. println ("S. indexof ('R'):" + S. indexof ('R '));
System. Out. println ("S. indexof ('R', 2):" + S. indexof ('R', 2 ));
System. Out. println ("S. indexof (SS):" + S. indexof (SS ));
Result: S. indexof ('R'): 1
S. indexof ('R', 2): 12
S. indexof (SS): 12
17.Int lastindexof (INT ch)
18.Int lastindexof (int ch, int fromindex)
19.Int lastindexof (string Str)
20.Int lastindexof (string STR, int fromindex)The above four methods are similar to the Methods 13, 14, 15, and 16. The difference is:Last matched content.
Public class comparetodemo {
Public static void main (string [] ARGs ){
String S1 = new string ("ACBDeBFG ");

System. Out. println (s1.lastindexof (INT )'B',7));
}
}
Running result:5
(WhereFromindexIs7, Is from the stringAcbdebfgThe last characterGStartForward number. It is a slave characterCStart matching and find the last matchB. So the result is5)

21.Int length ():Returns the length of the current string..
22.String Replace (char oldchar, char newchar):Replace the first oldchar in the string with newchar..
23.Boolean startswith (string prefix):Whether the string object starts with a prefix.
24.Boolean startswith (string prefix, int toffset):Indicates whether the string object starts with a prefix from the toffset position..
Example: String S = new string ("write once, run anywhere! ");
String Ss = new string ("write ");
String SSS = new string ("once ");
System. Out. println ("S. startswith (SS):" + S. startswith (SS ));
System. Out. println ("S. startswith (SSS, 6):" + S. startswith (SSS, 6 ));
Result: S. startswith (SS): True
S. startswith (SSS, 6): True
25.String substring (INT beginindex):Obtains the substring from the beginindex position to the end..
26.String substring (INT beginindex, int endindex):Obtain the substring from beginindex to endindex..
27.Char [] tochararray ():Converts a String object to a char array..
28.String tolowercase ():Converts a string to lowercase..
29.String touppercase (): Converts a string to uppercase.
Example: String S = new string ("Java. Lang. Class string ");
System. Out. println ("S. touppercase ():" + S. touppercase ());
System. Out. println ("S. tolowercase ():" + S. tolowercase ());
Result: S. touppercase (): Java. Lang. Class string
S. tolowercase (): Java. Lang. Class string
30.Static string valueof (Boolean B)
31.Static string valueof (char C)
32.Static string valueof (char [] data)
33.Static string valueof (char [] data, int offset, int count)
34.Static string valueof (double D)
35.Static string valueof (float F)
36.Static string valueof (int I)
37.Static string valueof (long l)
38.Static string valueof (Object OBJ)
The above method is used to convert different types into Java compiler type. These are all class methods.

 

 

Common Methods of the string class in Java:

Public char charat (INT index)

Returns the index character in the string;
Public int length ()
Returns the length of the string;
Public int indexof (string Str)
Returns the position where STR appears for the first time in the string;
Public int indexof (string STR, int fromindex)
Returns the position where STR appears for the first time from fromindex;
Public Boolean inclusignorecase (string another)
Compares the string with another (Case Insensitive );
Public String Replace (char oldchar, char newchar)
Replace the oldchar character with the newchar character in the string
Public Boolean startswith (string prefix)
Determines whether the string starts with a prefix string;
Public Boolean endswith (string suffix)
Determines whether a string ends with a suffix string;
Public String touppercase ()
Returns an uppercase string;
Public String tolowercase ()
Returns a string in lowercase.
Public string substring (INT beginindex)
Returns the child string from beginindex to the end;
Public string substring (INT beginindex, int endindex)
Returns the substring starting from beginindex to ending with endsindex.
Public String trim ()
Returns the string that removes the leading and trailing spaces from the string.
Public String [] Split (string RegEx)
Returns a string array separated by a specified separator.
Instance:
Public classSplitdemo{
Public static void main (string [] ARGs ){

StringDate= "2008/09/10 ";
String [] Dateaftersplit= New string [3];
Dateaftersplit=Date. Split ("/");// Use "/"Split the date string as a separator and put the result into three strings.

For (INT I = 0; I <Dateaftersplit. Length; I ++)
System. Out. Print (Dateaftersplit[I] + "");
}
}

Running result:2008 09 10 // The result is the split three strings

Instance:
Teststring1.java:
Program code
Public class teststring1
{
Public static void main (string ARGs []) {
String S1 = "Hello World ";
String S2 = "Hello World ";
System. Out. println (s1.charat (1 ));
System. Out. println (s2.length ());
System. Out. println (s1.indexof ("world "));
System. Out. println (s2.indexof ("world "));
System. Out. println (S1.Equals(S2 ));
System. Out. println (S1. Equalsignorecase(S2 ));

String S = "I Am a J2EE programmer ";
String sr = S. Replace ('I', 'you ');
System. Out. println (SR );
}
}

Teststring2.java:
Program code

Public class teststring2
{
Public static void main (string ARGs []) {
String S = "Welcome to Java World! ";
String S2 = "magci ";
System. Out. println (S. startswith ("welcome "));
System. Out. println (S. endswith ("world "));
String SL = S. tolowercase ();
String su = S. touppercase ();
System. Out. println (SL );
System. Out. println (SU );
String subs = S. substring (11 );
System. Out. println (subs );
String s1nosp = s2.trim ();
System. Out. println (s1nosp );
}

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.