Common methods for Java string Classes detailed introduction to _java

Source: Internet
Author: User
Tags array to string stringbuffer
String: Strings Type
first, the constructor
Copy Code code as follows:

String (byte[] bytes): Constructs a string object from a byte array.
String (char[] value): Constructs a string object from a char array.
String (Sting original): Constructs a copy of a original. That is: Copy a original.
String (stringbuffer buffer): Constructs a String object from an StringBuffer array.

For example:
Copy Code code as follows:

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
second, the method
Description: ①, all methods are public.
②, writing format: [modifier] < return type >< method name ([parameter list]) >
Example: Static int parseint (String s)
Indicates that this method (parseint) is a class method (static), the return type is (int), and the method requires a string type.
1. Char charAt (int index): takes one of the characters in a string, where the argument index refers to the ordinal in the string. The ordinal number of the string starts at 0 to length ()-1.
For example: string s = new String ("abcdefghijklmnopqrstuvwxyz");
System.out.println ("S.charat (5):" + S.charat (5));
Results: S.charat (5): F
2. int CompareTo (String anotherstring): The current String object is compared to anotherstring. The equality relationship returns 0; When unequal, the first unequal character difference is returned from the beginning of the No. 0 character of the two strings, and in the other case, the earlier part of the longer string happens to be a shorter string, returning their length difference.
3. int CompareTo (object o): If O is a string object, it is the same as the 2 function; otherwise the classcastexception exception is thrown.
For 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 length Difference
System.out.println ("S1.compareto (S3):" + S1.compareto (S3)); Returns the difference between ' k '-' a '
Results: S1.compareto (S2): 4
S1.compareto (S3): 10
4. String concat (String str): This string object is connected to Str.
5. Boolean contentequals (StringBuffer SB): Compare the String object with the StringBuffer object sb.
6. Static String copyvalueof (char[] data):
7. Static String copyvalueof (char[] data, int offset, int count): The two methods convert a char array to String, similar to one of the constructors.
8. Boolean endsWith (string suffix): Whether the string object ends with suffix.
For 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 the AnObject is not empty and is the same as the current string object;
Byte[] GetBytes (): Converts the string object to a byte array.
void GetChars (int srcbegin, int srcend, char[] DST, int dstbegin): This method copies a string into a character array. Where the srcbegin is the starting position of the copy, the srcend is the ending position of the copy, the string value DST is the target character array, and the Dstbegin is the copy starting position 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: I love you!
int hashcode (): Returns the hash table code for the current character.
int indexOf (int ch): Find only the first matching character position.
int indexOf (int ch, int fromindex): Start with Fromindex to find the first matching character position.
int indexOf (String str): Find only the first matching string position.
int indexOf (String str, int fromindex): Starts from Fromindex to find the first matching string position.
For 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
int lastindexof (int ch)
int lastindexof (int ch, int fromindex)
int LastIndexOf (String str)
LastIndexOf Int (String str, int fromindex) The above four methods are similar to 13, 14, 15, and 16, and the last match is found.
public class Comparetodemo {
public static void Main (string[] args) {
string S1 = new String ("ACBDEBFG");
System.out.println (S1.lastindexof ((int) ' B ', 7));
}
}
Run Result: 5
(where the Fromindex parameter is 7, is the number of digits from the last character, G, of the string ACBDEBFG.) It is both starting from character C and finding the position of the last match B. So the result is 5)
int length (): Returns the current string length.
Replace string (char Oldchar, char Newchar): Replaces the first oldchar in a string of symbols with Newchar.
Boolean startswith (String prefix): whether the string object starts with prefix.
Boolean startswith (string prefix, int toffset): This string object is calculated from the Toffset position, starting with prefix.
For 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
substring string (int beginindex): Takes a substring from the beginindex position to the end.
26.String substring (int beginindex, int endindex): Takes a substring from the beginindex position to the endindex position.
Char[] ToCharArray (): Converts the string object to a char array.
String toLowerCase (): Converts a string to lowercase.
String toUpperCase (): Converts a string to uppercase.
For example: string s = new String ("Java.lang.Class string");
System.out.println ("S.touppercase ():" + s.touppercase ());
System.out.println ("S.tolowercase ():" + s.tolowercase ());
The result is: S.touppercase (): JAVA. Lang. CLASS STRING
S.tolowercase (): java.lang.class string
Static String valueof (Boolean B)
Static String valueof (char c)
Static String valueof (char[] data)
Static String valueof (char[] data, int offset, int count)
Static String valueof (double D)
Static String valueof (float f)
static String valueof (int i)
Panax Notoginseng. Static String valueof (long L)
Static String valueof (Object obj)
The above methods are used to convert various types into Java characters. These are all class methods.
Common methods for string classes in Java:
public char charAt (int index)
Returns the index character in a string;
public int Length ()
Returns the length of the string;
public int indexOf (String str)
Returns the position of the first occurrence of STR in the string;
public int indexOf (String str,int fromindex)
Returns the position of the first occurrence of STR from the Fromindex start;
public boolean equalsignorecase (String another)
Compares strings with another (ignoring case);
Public String replace (char Oldchar,char Newchar)
Replace Oldchar characters with Newchar characters in strings
public boolean startswith (String prefix)
Determines whether a string begins with a prefix string;
public boolean endsWith (String suffix)
Determines whether a string ends with a suffix string;
Public String toUpperCase ()
Returns a string that is an uppercase form of the string;
Public String toLowerCase ()
Returns a string that is a lowercase form of the string
Public String substring (int beginindex)
Returns a substring of the string starting from Beginindex to the end;
Public String substring (int beginindex,int endindex)
Returns the substring of the string starting from Beginindex to the end of Endsindex
Public String trim ()
Returns the string after the string has been removed from the start and end spaces
Public string[] Split (String regex)
Separates a string by the specified delimiter, returning a delimited array of strings
Instance:
Copy Code code as follows:

public class splitdemo{
public static void Main (string[] args) {
String date = "2008/09/10";
String[] dateaftersplit= new string[3];
Dateaftersplit=date.split ("/"); Splits the date string with "/" as the delimiter and puts the result into 3 strings.
for (int i=0;i<dateaftersplit.length;i++)
System.out.print (dateaftersplit[i]+ "");
}
}

Run Result: 2008 09 10//result is a segmented 3 string
Instance:
Teststring1.java:
Program code
Copy Code code as follows:

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 Java EE programmer";
String sr = s.replace (' i ', ' you ');
System.out.println (SR);
}
}

Teststring2.java:
Program code
Copy Code code as follows:

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);
}
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.