1: String
(1) A sequence composed of multiple characters, called a string.
In life, many data descriptions use strings. In addition, we will operate on it.
Therefore, java provides such a class for our use.
(2) create a String object
A: String (): No parameter construction
String s = new String ();
B: String (byte [] bys): transmits a byte array as the parameter.
Byte [] bys = {97,98, 99,100,101 };
C: String (byte [] bys, int index, int length): converts a part of the byte array into a String.
Byte [] bys = {97,98, 99,100,101 };
String s = new String (bys, 1, 2 );
D: String (char [] chs): transmits an en character array as a parameter.
Char [] chs = {'A', 'B', 'C', 'D', 'E '};
String s = new String (chs );
E: String (char [] chs, int index, intlength): converts a part of the character array into a String.
Char [] chs = {'A', 'B', 'C', 'D', 'E '};
String s = new String (chs, 1, 2 );
F: String (String str): transmits a String as a parameter.
Char [] chs = {'A', 'B', 'C', 'D', 'E '};
String s = new String (chs, 1, 2 );
String ss = new String (s );
G: directly assign a string constant to the string reference object.
String s = "hello ";
(3) Interview Questions
A: I have created several objects in String s = new String ("hello.
Two. A "hello" String object and an s object.
B: Write the following results.
String s1 = newString ("abc ");
Strign s2 = newString ("abc ");
String s3 = "abc ";
String s4 = "abc ";
Sop (s1 = s2); // false
Sop (s1 = s3); // false
Sop (s3 = s4); // true
C: Once a String object is created, it cannot be changed.
The constant value of a string does not change.
(4) methods for various functions in strings
A: Judgment
Boolean equals (Object anObject): determines whether the content of the two strings is the same
Boolean inclusignorecase (String anotherString): determines whether the content of the two strings is the same, case insensitive
Boolean contains (String s): determines whether a String contains another String.
Boolean endsWith (Stringsuffix): Tests whether the string ends with a specified suffix.
Boolean startsWith (Stringsuffix): test whether the string starts with the specified prefix.
Boolean isEmpty (): test whether the string is null
B: obtain
Int length (): returns the length of this string.
Char charAt (int index): returns the char value at the specified index.
Int indexOf (int ch): returns the index of the first occurrence of the specified character in this string.
Int indexOf (int ch, intfromIndex): returns the index that appears at the specified character for the first time in this string, starting from the specified index.
Int indexOf (String str): returns the index of the first occurrence of the substring in this String.
Int indexOf (String str, intfromIndex): returns the index of the first occurrence of the substring from the specified index.
Int lastIndexOf (int ch): returns the index of the last occurrence of the specified character in this string.
Int lastIndexOf (int ch, intfromIndex)
Returns the index of the last occurrence of the specified character in this string. reverse search starts from the specified index.
Int lastIndexOf (String str)
Returns the index at the rightmost of the substring.
Int lastIndexOf (String str, int fromIndex)
Returns the index of the last occurrence of the substring in this string, and returns the reverse search from the specified index.
Stringsubstring (int beginIndex)
Returns a new string, which is a substring of this string.
String substring (intbeginIndex, int endIndex)
Returns a new string, which is a substring of this string.
C: Conversion
Byte [] getBytes (): method from string to byte array
Char [] toCharArray (): method from string to character array
Static StringcopyValueOf (char [] data)
Returns the String that represents the character sequence in the specified array.
Static StringcopyValueOf (char [] data, int offset, int count)
Returns the String that represents the character sequence in the specified array.
StaticString valueOf: converts data of this data type to a string.
String toLowerCase (): converts a String to lowercase.
String toUpperCase (): converts a String to uppercase.
String connection
String concat (String str): connects the specified String to the end of the String.
D: replace
String replace (charoldChar, char newChar): replace the old character with a new character
String replace (String target, String replacement): replace the old String with a new substring.
E: Segmentation
String [] split (Stringregex): Splits a String into a String Array Based on the specified String.
F: Stringtrim (): removes leading and trailing spaces of a string.
G: intcompareTo (String anotherString)
Compares two strings in alphabetical order.
IntcompareToIgnoreCase (String str)
Compares two strings in alphabetical order, regardless of case.
Author: longchengjia1114