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. The Java environment provides string and StringBuffer two classes for storing and maintaining these two types of strings.
Some common action strings for string creation
String str="This is a String";
Or
String str=newString("This is a String");
The first kind of efficiency is higher, detailed can be seen:
String, StringBuffer and StringBuilder differences and contact (source code)
String length
str"This is a String";int len =str.length();
Specify the position of a character or substring
str="Thisis a String";Int index1 =str.indexOf("i"); //2index2=str.lastIndexOf("i"//12
Determine if two strings are equal
String str="This is a String";Boolean result=str.equals("This is another String");
Gets the character at the specified position
str="This is a String";char chr=str.charAt(3);
Truncate substring
str=str.substring(int beginIndex);
Intercept STR from the first letter of the length of the Beginindex string, the remaining string is assigned to STR;
str=str.substring(int beginIndex,int endIndex);
Intercepts the string from Beginindex to the end of Endindex in STR and assigns it to STR;
String merge
str="This is a String";String str1=str.concat("Test"//str1="This is a String Test"
string-Case conversions
str
Uppercase to lowercase
str
lowercase to uppercase
Remove spaces at the beginning and end of a string
str="This is a String ";String str1=str.trim(); //str1="This is a String"
Some common operations for StringBuffer initialization
StringBuffer s = new StringBuffer();
This initializes the StringBuffer object as an empty object.
If you need to create a StringBuffer object with content, you can use:
StringBuffer s = new StringBuffer(“abc”);
Append method
Appends content to the end of the current StringBuffer object, similar to a string-like connection. After the method is called, the contents of the StringBuffer object also change.
new StringBuffer(“abc”); sb.append(true);
The value of the object SB will become "Abctrue"
Deletecharat method
Deletes the character at the specified position and then forms the remaining content into a new string
StringBuffer sb = new StringBuffer(“Test”); sb. deleteCharAt(1);
The value of the object SB becomes "Tst"
There is also a function-like Delete method:
delete(int start,int end)
The function of this method is to delete all characters within the specified interval, including the start, which does not contain the range of the end index value.
Insert method
Inserts the contents into the StringBuffer object and then forms a new string.
StringBuffer sb = new StringBuffer(“TestString”); sb.insert(4,false);
When you insert a false value at the index value 4 of the object SB, and form a new string, the value of the object SB after execution is "testfalsestring".
Reverse method
Reverses the contents of the StringBuffer object and forms a new string
new StringBuffer(“abc”); sb.reverse();
The contents of the object SB will become "CBA"
Setcharat method
Modifies the character of the specified index value position in the object to a new character
StringBuffer sb = new StringBuffer(“abc”); sb.setCharAt(1,’D’);
The value of SB will become "ADc"
TrimToSize method
Reduce the waste of space by narrowing the storage space of the StringBuffer object to the same length as the string length.
Problems
string comparison, using "= =" or equals ()?
Source analysis of the difference between equals and = =
Use string as the case condition in a switch statement?
Can switch use string to make arguments?
Convert string to Numeric
int age = Integer.parseInt("10"); longid = Long.parseLong("190"// 假如值可能很大.
Very large numbers please use a long
- Splitting a string with whitespace characters
String[] strArray = aString.split("\\s+");
Strings received by the split () method of string are parsed as regular expressions.
"\s" represents a blank character, such as a Space "" (Tab tab "\ T", newline "\ n", carriage return "\ r")
The compiler also takes a literal transcoding when parsing the source code, so "\s" is required.
- Convert a string to a date
format = new SimpleDateFormat("yyyy-MM-dd"); "2016-05-07"; dateformat.parse(str); System.out.println(format.format(date));
[Data structure] string manipulation