Java Learning Note (string application, StringBuffer class, StringBuilder Class)

Source: Internet
Author: User

1. Gets the number of case and number in the specified string:

 Packagedemo; Public classStringtest { Public Static voidMain (string[] args) {GetCount ("IamHandsome666"); }     Public Static voidGetCount (String str) {intUpper = 0; intLower = 0; intDigit = 0;  for(inti = 0; I < str.length (); i++) {            Charc =Str.charat (i); if(c >= && C <= 90) {Upper++; } Else if(c >= && C <= 122) {Lower++; } Else if(c >= && c <= 57) {digit++; }} System.out.println (upper);//2System.out.println (lower);//9SYSTEM.OUT.PRINTLN (digit);//3    }}

2. Turn the first letter in a string into uppercase, and the rest of the letters to lowercase and print

 Packagedemo; Public classStringtest { Public Static voidMain (string[] args) {string string= Toconvert ("Iamhandsome");        System.out.println (string); //Output: Iamhandsome    }     Public Staticstring Toconvert (String str) {string First= Str.substring (0,1); String After= str.substring (1); First=first.touppercase (); after=after.tolowercase (); returnfirst+After ; }}

3. Find the number of occurrences of a small string from a long string:

 Packagedemo; Public classStringtest { Public Static voidMain (string[] args) {System.out.println (Getstringcount ("Ilikejava,andjavaisthebest,java", "Java")); }     Public Static intGetstringcount (String str, string key) {intCount = 0; intindex = 0;  while(index = Str.indexof (key))! =-1) {Count++; STR= Str.substring (index+key.length ()); }        returncount; }}//Output: 3

String strings cannot be changed, there are some inconveniences

So introduce a new class

StringBuffer class, String buffer

Purpose of appearance: in order to improve the efficiency of string manipulation

Internally, a variable array method is used, and an array is defined inside the class, and the array is not final

The default capacity of the array is 16

For its approach, here is an example to understand:

 Packagedemo; Public classStringbufferdemo { Public Static voidMain (string[] args) {append ();        Delete ();        Insert ();        Replace ();        Reverse ();    Tostring_ (); }         Public Static voidappend () {StringBuffer buffer=NewStringBuffer (); Buffer.append (6); Buffer.append ("Hello");        SYSTEM.OUT.PRINTLN (buffer); //6hello    }     Public Static voidDelete () {StringBuffer buffer=NewStringBuffer (); Buffer.append ("Helloilikejava"); Buffer.delete (1, 2); Buffer.deletecharat (8);        SYSTEM.OUT.PRINTLN (buffer); //Hlloilikjava    }     Public Static voidInsert () {StringBuffer buffer=NewStringBuffer (); Buffer.append ("Java"); Buffer.insert (1, "Python");        SYSTEM.OUT.PRINTLN (buffer); //Jpythonava    }     Public Static voidreplace () {StringBuffer buffer=NewStringBuffer (); Buffer.append ("ABCDEFG"); Buffer.replace (2, 4, "H");        SYSTEM.OUT.PRINTLN (buffer); //ABHEFG    }     Public Static voidreverse () {StringBuffer buffer=NewStringBuffer (); Buffer.append ("ABCDEFG");        Buffer.reverse ();        SYSTEM.OUT.PRINTLN (buffer); //GFEDCBA    }     Public Static voidTostring_ () {StringBuffer buffer=NewStringBuffer (); Buffer.append ("ABCDEFG"); String String=buffer.tostring ();        System.out.println (string); //outputs a string of type ABCDEFG, which is a string    }}

StringBuffer class Instances:

 Public classStringbuffertest { Public Static voidMain (string[] args) {int[] arr = {4,1,4,56,7,8,76};    System.out.println (toString (arr)); }   /** Purpose: * int[] arr = {34,12,89,68}; Convert element in a int[] to String * format [34,12,89,68]*/     Public StaticString toString (int[] arr) {        //Creating a String bufferStringBuffer buffer =NewStringBuffer (); Buffer.append ("["); //Array Traversal         for(inti = 0; I < arr.length;i++){            //Judging is not the last element of the array            if(i = = Arr.length-1) {buffer.append (Arr[i]). Append ("]"); }Else{buffer.append (Arr[i]). Append (","); }        }        returnbuffer.tostring (); }}

There is also a StringBuilder class, and the method is exactly the same as the StringBuffer method

Difference:

The StringBuffer class is a thread-safe class, and the StringBuilder class is a thread-insecure class, but it is faster

Threading knowledge is going to be in the back.

The StringBuilder class is recommended for daily development, as relative speed is faster

Java Learning Note (string application, StringBuffer class, StringBuilder Class)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.