The string class represents strings, and all string classes in Java are implemented by his definition. The essence of a string is a character array
" D ";
Common construction methods for string classes
byte [] by =new byte []{99 , 98 , , style= "color: #800080" >96 }; Create a character set, assign to str print String str =new string (by ); System. out . println (str);
1 byte [] by =newbyte[]{99,98,97,96}; 2 String str =new string (by,1,3); From index 1th to 3 bits, contain 1-bit and 3-bit. Print output, with string (character set name, starting from the beginning, to the end of the first) 3 System.out.println (str);
Char [] chars =newchar[]{' A ', ' B ', ' C ', ' d '}; =new String (chars,1,3); From 1th to 3rd place, with 1th and 3rd, usage and above basic consistent System.out.println (str);
String str =new string ("adb"); System.out.println (str);
Common methods of the string class
How many characters are in a string
String str =new string ("1,2,3,4"); System.out.println (Str.length ());
Get partial string
1 String str =new string ("1,2,a,b,c,d,"); Create a string, from index 1 to index 3, with 1-bit and 3-bit, comma also account for one-bit index of 2 System.out.println (str.substring (1, 4));
Whether to contain another string in the string
1 String str =new string ("1,2,a,b,c,d,"); When this string number has a number, it returns ture, otherwise false2 System.out.println (Str.contains ("3"));
1 String str =new string ("1,2,a,b,c,d,"); Determine if there is no string in this string array, and if there is an index value showing the string, no return-12 System.out.println (Str.indexof ("C"));
Gets the contents of the object in the string
1 String str =new string ("1,2,a,b,c,d,"); 2 System.out.println (str.tostring ()); As a result 3 System.out.println (str);
Little Practice
1. A string that determines whether the contents of the string are empty
1 String str =new string (); 2 System.out.println (Str.isempty ());
2. Gets the position of the given character, where it first appears in the string
String str =new string ("1,2,a,b,c,d,"); System.out.println (Str.indexof ("2"));
3. Gets the character at the specified position in the string
1 String str =new string ("1,2,a,b,c,d,"); 2 System.out.println (Str.substring (2, 5));
4. convert the string to a lowercase string
1 String str =new string ("A,b,c,d"); 2 System.out.println (Str.tolowercase ());
5. convert the string to uppercase string
1 New String ("A,b,dad,da"); 2 System.out.println (Str.touppercase ());
6. In the string, replace the given old character with the new character
1 New String ("A,b,dad,da"); 2 System.out.println (Str.replace ("A", "Wang"));
7, remove the string at both ends of the space, the middle will not be removed, return a new string
1 New String (" a,b,dad,da"); 2 System.out.println (Str.trim ());
8, Title one: Gets the number of uppercase, lowercase, and numbers in the specified string.
L Idea:1. In order to count the number of uppercase letters, lowercase characters, and numbers. Create a variable of 3 counts.
In order to get to each character in the string, the string is traversed to get each character.
The resulting character is judged, and if the character is uppercase, the number of uppercase letters is +1;
If the character is lowercase, the number of lowercase letters is +1, and if the character is a number, the number is +1.
Displays the number of uppercase, lowercase, and digits
1String str =NewString ("a,2,c,d,3");2 intA =0;3 intb =0;4 intC =0;5 for(intI=0;i<str.length (); i++){6 CharCH =Str.charat (i);7 if(ch>= ' A ' && ch<= ' Z '){8a++;9}Else if(Ch >= ' 0 ' && ch<= ' 9 '){Tenb++; One}Else if(ch>= ' A ' && ch<= ' z '){ AC++; - } - the } -System.out.println (A + "\ t" +b+ "\ T" +c);
Java String class