Java Study Notes 20 (String application, StringBuffer, StringBuilder ),
1. Obtain the numbers of uppercase and lowercase letters and numbers in a specified string:
package demo;public class StringTest { public static void main(String[] args) { getCount("IamHandsome666"); } public static void getCount(String str) { int upper = 0; int lower = 0; int digit = 0; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c >= 65 && c <= 90) { upper++; } else if (c >= 97 && c <= 122) { lower++; } else if (c >= 48 && c <= 57) { digit++; } } System.out.println(upper);//2 System.out.println(lower);//9 System.out.println(digit);//3 }}
2. Convert the first letter in a string to uppercase, And the other letters to lowercase and print
Package demo; public class StringTest {public static void main (String [] args) {String string = toConvert ("iAMhaNdSoMe"); System. out. println (string); // output: Iamhandsome} public static String toConvert (String str) {String first = str. substring (0, 1); String after = str. substring (1); first = first. toUpperCase (); after = after. toLowerCase (); return first + after ;}}
3. Find the number of occurrences of a small string from a long string:
Package demo; public class StringTest {public static void main (String [] args) {System. out. println (getStringCount ("Ilikejava, andjavaisthebest, java", "java");} public static int getStringCount (String str, String key) {int count = 0; int index = 0; while (index = str. indexOf (key ))! =-1) {count ++; str = str. substring (index + key. length ();} return count ;}// output: 3
The String cannot be changed, causing some inconvenience.
So introduce a new class
StringBuffer class, String Buffer
Objective: To improve the efficiency of string operations
The variable array method is used internally, and the class defines an array internally. This array does not have final
The default size of the array is 16.
Here is an example of its method:
Package demo; public class StringBufferDemo {public static void main (String [] args) {append (); delete (); insert (); replace (); reverse (); toString _ ();} public static void append () {StringBuffer buffer = new StringBuffer (); buffer. append (6); buffer. append ("hello"); System. out. println (buffer); // 6 hello} public static void delete () {StringBuffer buffer = new StringBuffer (); buffer. append ("helloIlikeJava"); buffer. delete (1, 2); buffer. deleteCharAt (8); System. out. println (buffer); // hlloIlikJava} public static void insert () {StringBuffer buffer = new StringBuffer (); buffer. append ("java"); buffer. insert (1, "Python"); System. out. println (buffer); // jPythonava} public static void replace () {StringBuffer buffer = new StringBuffer (); buffer. append ("abcdefg"); buffer. replace (2, 4, "H"); System. out. println (buffer); // abHefg} public static void reverse () {StringBuffer buffer = new StringBuffer (); buffer. append ("abcdefg"); buffer. reverse (); System. out. println (buffer); // gfedcba} public static void toString _ () {StringBuffer buffer = new StringBuffer (); buffer. append ("abcdefg"); String string = buffer. toString (); System. out. println (string); // output a String-type abcdefg, that is, a string }}
StringBuffer class instance:
Public class StringBufferTest {public static void main (String [] args) {int [] arr = {,}; System. out. println (toString (arr);}/** objective: * int [] arr = {34,12, 89,68}; convert an element in an int [] into a string * Format [, 12, 89,68] */public static String toString (int [] arr) {// create a String buffer StringBuffer buffer = new StringBuffer (); buffer. append ("["); // array traversal for (int I = 0; I <arr. length; I ++) {// determine if it is the last element of the array if (I = arr. length-1) {buffer. append (arr [I]). append ("]");} else {buffer. append (arr [I]). append (",") ;}} return buffer. toString ();}}
There is also a StringBuilder class, which has the same method as the StringBuffer method.
Differences:
The StringBuffer class is a thread-safe class, And the StringBuilder class is a thread-insecure class, but it is faster.
Thread knowledge will be discussed later,
We recommend that you use the StringBuilder class for daily development because it is relatively faster.