public class stringtest{
/*
* 1, String class: String.
* 1) What is a string?
* A series of characters that enclose one or more characters in a pair of double quotation marks. For example: "A" "ABC"
* Special string: A pair of consecutive double quotes. That is: "", which is called an empty string. Also known as a string of length 0.
*
* 2) Character of the string:
* a) A string is a reference type, and a string is an object.
* b) The string is invariant.
* c) Each character in the string has an index number (position number). It starts from 0 to length ()-1. Where: the length () method is used to find the lengths of the strings.
* For example: "ABCDEFG" where the index number of the character a is 0, and the index number of G is 6; the length of the string is () ===>>> 7.
* d) in memory, a string can be shared. That is, when multiple identical strings are assigned to a string variable, there is only one string in memory.
* For example: String S1 = "ABC"; String s2 = "ABC"; Then S1 and S2 correspond to the same string.
* e) Strings have many methods that can be used to solve many practical problems.
*
* 3) Method of string:
* Length ()
* CHARAT (n);
* SUBSTRING (n);
* SUBSTRING (N,M);
* INDEXOF (S2);
* INDEXOF (s2, N);
* LASTINDEXOF (S2);
*/
public static void Main (string[] args) {
String S1 = ""; Initialize to an empty string.
int len = S1.length (); S1 the length of the corresponding string.
System.out.println ("S1 corresponding to the string: \" "+ S1 +" \ "+", its length is: "+ len);
String s2 = null; A null at this point indicates that the string does not exist.
Len = S2.length (); S2 the length of the corresponding string. Cause of the error: Because the string object does not exist, therefore, the method of not calling the string length ();
S1 = "ABCDEFG";
char ch = s1.charat (0); Gets the character on the No. 0 position in the string
System.out.println ("\" "+ S1 +" \ "" + ", the string in the No. 0 position of the character is \ '" + ch + "\");
ch = S1.charat (S1.length ()-1); Take the last character
String sub1 = s1.substring (3); From the 3rd position in the S1 string, start at the end and make the characters a substring.
System.out.println ("sub1>>>" + sub1);
Sub1 = s1.substring (2). substring (3);
System.out.println (SUB1);
Sub1 = s1.substring (2, 5); All characters starting from the 2nd position to 5-1 positions are fetched and formed into a substring.
System.out.println (SUB1);
S2 = "Beijing" + 2008; Connection: Converts 2008 to a string before connecting.
System.out.println ("s2=\" "+ s2 +" \ "");
S1 = "I Love java!" I Love beijing! ";
S2 = "Love";
int loc = S1.indexof (s2); Find S2 in order in S1, and if present, feedback the position number where it first appears. If not present, a negative number is fed back.
if (loc >= 0) {
System.out.println ("in \" "+ S1 +" \ "to find \" "+ s2 +" \ "+", found. It is in the "+ loc +" position. " );
}
loc = S1.indexof (s2, 7); Find from 7th position in S1
System.out.println (">>>" + loc);
loc = S1.lastindexof (s2); Find the S2 in S1 and feedback the position number where it last appeared.
S2 = S1.replace ("Love", "^_^");
System.out.println ("Original string: s1>>" + S1);
System.out.println ("New string: s2>>" + s2);
S2 = s1.replace (' O ', '? ');
System.out.println ("Original string: s1>>" + S1);
System.out.println ("New string: s2>>" + s2);
S1 = "I love java!";
Boolean flag = S1.startswith ("i"); Judging S1 's string is "I" Start?
System.out.println ("flag>>" + flag);
Flag = S1.endswith ("java");
System.out.println ("flag>>" + flag);
S1 = "Beijing Metro 2015. jpg";
S1 = S1.touppercase (); The letters in all strings are converted to uppercase.
S2 = "JPG";
S2 = S2.touppercase ();
Flag = S1.endswith (s2);
System.out.println ("flag>>>" + flag);
S1 = "I love java!";
System.out.println ("\ns1>>" + S1);
S1 = S1.tolowercase (); All the letters in the string are converted to lowercase.
System.out.println ("s1>>" + S1);
S1 = "China" + "Beijing" + 2008;
System.out.println ("\ns1>>" + S1);
S1 = "China" + "Beijing". Trim () + 2008; Trim () is used to delete spaces before and after a string.
System.out.println ("\ns1>>" + S1);
}
}