String: Strings Type
1. Constructor function.
String (): Constructs an empty string object.
String (byte[] bytes): Constructs a string object from a byte array.
String (byte[] bytes, int offset, int length): The string object is constructed with a byte array, starting with offset, and a total length of bytes.
String (char[] value): Constructs a string object from a char array.
String (char[] value, int offset, int count): The string object is constructed by a char array, starting with offset, with a total length of bytes.
String (string original): Constructs a copy of a original. Both, copy a original.
String (stringbuffer buffer): Constructs a String object by StringBuffer an array;
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);
String sb_sub = new string (b,3,2);
String sc = new string (c);
String sc_sub = new string (c,3,2);
String sb_copy = new string (SB);
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);
The results are:
Sb:abcdefghij
Sb_sub:de
sc:0123456789
Sc_sub:34
Sb_copy:abcdefghij
2, method.
Description
1. All methods are public;
2. Writing format: [modifier] < return type > < method name ([parameter list]) >
Such as:
The static int parseint (string s) means 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.
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.
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));/return ' K '-' a ' to the poor
The results are:
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.
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.
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.
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));
The results are:
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.
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.
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));
The results are:
S.startswith (ss): True
S.startswith (sss,6): True
substring string (int beginindex): Takes a substring from the beginindex position to the end.
substring string (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.
string s = new String ("Java.lang.Class string");
System.out.println ("S.touppercase ():" + s.touppercase ());
System.out.println ("S.tolowercase ():" + s.tolowercase ());
The results are:
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.