I. Basic concepts of Java string classes
In the Java language, string data is actually implemented by the string class. There are two classes of Java string classes: One is the immutable string that is not changed in the program, and the second class is a variable string that will be changed in length in the program. In order to store and maintain these two types of strings, the Java environment providesStringAndStringBufferTwo of classes.
first, create a string
Cases:Stringstr=New"This is a String");
OrStringstr="This is a String";
Second, get the information about the string object
1. By callingLength ()method to get the length of the string.
Cases:
String str="Thisis a String";
int Len =str.length ();
of the 2.StringBuffer classcapacity ()Method with the String class.Length ()is similar, but she tests the size of the memory space allocated to StringBuffer, not the memory space currently being used.
3. If you want to determine the position of a specified character or substring in a string in a given string, you can use theindexOf ()AndlastIndexOf ()Method.
String str="Thisis a String";
Int index1 =str.indexof ("I"); //index=2
Intindex2=str.indexof (' i ', index+1); Index2=5
Intindex3=str.lastindexof ("I"); Index3=15
Intindex4=str.indexof ("String"); index4=10
Iii. comparison and manipulation of string objects
1. Comparison of String objects
The Equals () method of the string class is used to determine whether two strings are equal.
String str="Thisis a String";
Boolean Result=str.equals ("This is another String");
//result=false
2. Access to a String object
A, methodcharAt ()The character used to get the specified position.
String str="Thisis a String";
Char Chr=str.CharAt (3); //chr= "I"
B, methodsGetChars ()A part of the string used to get the string
PublicVoidgetchars (int Srcbegin,Intsrcend,CHAR[]DST,Intdstbegin)
String str="Thisis a String";
Char CHR =NewCHAR[10];
Str.getchars (5,12,chr,0); //chr= "Isa St"
CsubString ()is another way to extract a string that specifies where to begin extracting the string and where to end it.
3. Manipulating strings
Areplace ()method to replace one character in a string with another character.
String str="Thisis a String";
String str1=str.replace (' t ', ' t ');Str1= "Thisis a String"
The B, concat () method can combine two strings into a single string.
String str="Thisis a String";
String Str1=str.concat ("Test"); //str1= "thisis a String Test"
CtoUpperCase ()AndtoLowerCase ()The Convert method implements string-case conversions, respectively.
String str="Thisis A STRING";
String str1=str.tolowercase (); //str1= "thisis a string";
The D, Trim () method removes the spaces at the beginning and end of the string.
string str= "thisis a string ";
string Str1=str.trim (); //str1= "This is a String "
E, String class provides a static method valueOf () , It can convert any type of data object to a string. such as
System.out.println (string,valueof (MATH,PI));
Four, modifying a mutable string
The StringBuffer class provides 3 methods for modifying a mutable string, inserting and changing the character of a position in the middle of a string.
1. Append after string: Use the append () method to add various objects to the string.
2. Insert in the middle of the string: Use the Insert () method. Example
StringBuffer str=" Thisis A String ");
Str.insert (9, System.out.println (Str.tostring ());
This code output is: Thisis a test String
3. Change the character of a position, using the Setcharat () method.
StringBuffer SB =new stringbuffer ("aaaaaa");
Sb.setcharat (2, "B"); Result AABAAA
Summary of Java String Operations 1 (reproduced)