Java, Java String
/*
The String class is used to describe String objects.
It provides multiple methods to operate strings.
All methods are used, and the string is over.
What are common operations?
"Abcd"
What functions should it provide so that we can better operate it?
1. Obtain (must be mastered)
The number of characters contained in a 1.1 string, that is, the length of the string.
Int length () but the array also has length. The length called by the array is not in parentheses and belongs to the attribute, but the string is a method.
1.2 obtain a character at a position based on its location
Char charAt (int index)
1.3 obtain the position of the character in the string based on the Character
Int indexof (int ch) returns the position where the character ch first appears in the string.
Int indexpf (int ch, int fromIndex) obtains the position where ch appears in the string starting from the position specified by fromIndex.
Int indexof (String str) returns the position where str appears for the first time in the String.
Nt indexpf (String str, int fromIndex) obtains the position where str appears in the String starting from the position specified by fromIndex.
Reverse index ---- add one more last.
2. Judgment
2.1 whether the string contains a substring
Boolean contains (str );
Int indexof (int ch) returns the position where the character ch appears for the first time in the string.
2.2 whether the string contains content
Boolean isEmpty (); the principle is to determine whether the length is 0
2.3 whether the string starts with the specified content
Boolean startWith (str)
2.4 whether the string starts with the end of the specified content and determines whether the end is of the bool type
Boolean endWith (str)
2.5 check whether the content of the string is the same and the object method in the parent class is rewritten
Boolean equals (str );
2.6 determine whether the content is the same and ignore the case sensitivity
Boolean inclusignorecsae ();
3. Conversion
3.1 convert character arrays into strings
Constructor String (chae [])
String (char [], int count) converts a part of the character array into a String
Static Method static String copyValue (char []);
Static String copyValue (char [], int count );
Static String valueof (char []);
3.2 convert a string into a character array
Char [] toCharArray ();
3.3 convert the byte array into a string
String (byte [])
String (byte [], int count)
Anti-byte [] getBytes ();
3.4 convert a string into a byte array
3.5 convert the basic data type to a string
Static String valueof (int );
Static String valueof (double );
Special:
The encoding table can be specified during conversion of string and byte arrays.
4. Replace
String replace (oldchar, newchar); replace a String.
5. Cutting
String [] split (regex); cut the character and assemble each part into an array
6. substring to obtain part of the string
String substring (begin); begin is a number from the specified position to the end of 0 1 2 If the badge does not exist, an exception occurs.
String substring (begin, end); contains the header, not the end
7. Comparison of spaces in conversion Removal
7.1 convert string to uppercase or lowercase
String toUppercase (); this is why java ignores case sensitivity.
String toLowercase ();
7.2 remove unnecessary spaces at both ends of the string
String trim ();
7.3 compare the natural order of two strings
Int compareTo (); start to compare, starting from 0, to compare, find different, then subtract, return this value
*/
Class StringMethodDemo
{
Public static void method_get ()
{
String str = "abcdefakbf ";
/* Length */
// System. out. println (str. length ();/* What if there is another string? Is it necessary to print n times? What should I do? P1 */
Sop (str. length ());
/* Obtain Characters Based on the Index */
Sop (str. charAt (40);/* an exception occurs when a non-existent badge in the string is accessed */
Sop (str. charAt (4 ));
/* Obtain the index based on characters */
Sop (str. indexof ('A '));
Sop (str. indexof ('A', 3 ));
Sop (str. indexof ('V', 3);/* if not found, the returned value is-1 and no exception will occur */
/* Reverse index ---- Method */
Sop (str. lastIndexof ("a");/* the output badge will not change, starting from the left */
}
Public static void sop (Object obj)/* print whatever value is passed in */
{
System. out. println (obj );
}
Public static void main (String args [])
{
/*
String s1 = "abc ";
String s2 = new String ("abc ");
String s3 = "abc ";
System. out. println (s1 = s2); false
System. out. println (s1 = s3); true? Why? Abc exists in the constant pool, and s3 will no longer open up memory space after it finds abc exists.
*/
/* P1 */
Method_get ();
}
}