Java Learning Note 05--string.
First, the String class
A string of literal symbols made up of characters are called strings. in Java, a string is not just an array of characters, but is an instance of the string class that can be constructed using the string class.
Each character of a string is constructed using Unicode characters.
Several methods on the Sring object:
Length () Gets the lengths of the strings
Equals () determines whether the characters in the source string are equal to the characters in the specified string
toLowerCase () converts the English characters in a string to lowercase
toUpperCase () converts the English characters in the string to uppercase
The nature of a string is made up of character arrays, so when you declare a string using the String class, the string has the properties of the array index.
char charAt (int index) returns the character at the specified index
int indexOf (int ch) returns the first index position found by the specified character
int indexOf (String str) returns the first index position found for the specified string
int lastIndexOf (int ch) returns the last index position found for the specified character
string substring (int beginindex) extracts substrings at the specified index to the end of the string
string substring (int beginindex, int endIndex) takes out the specified index range substring ( including beginindex, not including EndIndex)
Char[] ToCharArray () converts a string to a character array
[Java]View Plaincopy
- Public class T {
- public static void Main (string[] args) {
- String str = "Hello itmyhome";
- For (int i=0;i<str.length (); i++) {
- System.out.print (Str.charat (i));
- }
- System.out.println ();
- System.out.println ("First O index Position:" +str.indexof ("O"));
- System.out.println ("Last O index Position:" +str.lastindexof ("O"));
- System.out.println ("intercept from the 6th character to the tail:" +str.substring (6));
- System.out.println ("intercept from 6th to 8th characters:" +str.substring (6, 8));
- char charstr[] = Str.tochararray ();
- For (int i = 0; i < charstr.length; i++) {
- System.out.print (Charstr[i]);
- }
- }
- }
Execution Result:
[Java]View Plaincopy
- Hello Itmyhome
- First O index position: 4
- Last O index position: one
- Intercept from the first 6 characters to the end: Itmyhome
- Intercept from 6 to 8 characters: it
- Hello Itmyhome
When building a string object, you can use a character array to build it, in addition to specifying string constants directly after = using "":
[Java]View Plaincopy
- Char name[] = {' i ',' t ',' m ',' y ',' h ',' o ',' m ',' e '};
- string s = new string (name);
Second, immutable string
A very important rule to use strings in Java is to remember that once a string object is configured, its contents are fixed and immutable.
In Java, use "=" To assign a string object to a reference name, meaning to change the object that the name refers to, and if the original referenced string object has no other name to reference it, it will be recycled by Java's garbage collection mechanism when appropriate.
In Java execution, a string pool is maintained, and for some string objects that can be shared, there is now a string pool looking for the same string content (the same character), and if so, return directly instead of creating a new string object directly. To reduce the memory consumption. If you declare it in your program in the following way, you are actually pointing to the same string object:
[Java]View Plaincopy
- String str1 = "Itmyhome";
- String str2 = "Itmyhome";
- System.out.println (STR1==STR2);
When you use "" in a program directly to include a string, the string is in the string pool.
StringBuilder class
Reference http://blog.csdn.net/itmyhome/article/details/7232340
Separating strings
You can use the split () string to separate strings
Note the question : ".", "|", "? "is an escape character and must be added" \ \ "
[Java]View Plaincopy
- String s1[] = str.split ("\ \");
- String s2[] = Str.split ("\\|");
- String s3[] = str.split ("\ \");
If you have more than one delimiter in a string, you can use the ' | ' As a hyphen.
[Java]View Plaincopy
- String str = "Welcome?to!itmyhome Bolg";
- String s[] = Str.split ("\\?|!");
- For (int i = 0; i < s.length; i++) {
- System.out.println (S[i]);
- }
To? And! As a separator character.
Java Learning Note 05--string.