Java Tour (17)--stringbuffer Overview, store, delete, get, modify, invert, store buffer data into an array, StringBuilder

Source: Internet
Author: User

Java Tour (17)--stringbuffer Overview, store, delete, get, modify, invert, store buffer data into an array, StringBuilder

After we finish the string, let's talk about his little brother.

I. StringBuffer overview

What does buffer mean about the object of StringBuffer? The meaning of the buffer, string once initialized can not be changed, and StringBuffer is possible, which is the difference, features:

    • StringBuffer is a container
    • Multiple data types can be manipulated in bytes
    • Will eventually become a string through the ToString method

    • Store

      StringBuffer append (): Adds the specified data as a parameter to the end of the existing data

    • Delete

      StringBuffer Delete (start, end) removes data from the buffer, containing start, without end
      StringBuffer Deletecharat (index) deletes the character at the specified position

    • Get

      char charAt (int index)
      int indexOf (String str)
      int Lasrindexof (String str)
      String subString (int start,int end)

    • Modify

      StringBuffer Replace (start,end,string)
      void setchatat (int dex,char ch)

    • Reverse

      String reverse ()

    • Storing the data in the buffer in an array

      void GetChars (int srcbegin,int srcend, char[] dst,int dstbegin)

With this trait, let's take it a step at a moment.

 PackageCom.lgl.hellojava;//public class class name Public  class Hellojjava {     Public Static void Main(string[] args) {/** * StringBuffer * *StringBuffer SB =NewStringBuffer (); StringBuffer append = Sb.append ( +);        SOP (SB = = append);        SOP (Sb.tostring ());    SOP (Append.tostring ()); }/** * Output * *     Public Static void SOP(Object obj)        {System.out.println (obj); }    }

This is more common, we don't need to be so troublesome, we can simplify

sb.append("abc").append(36);sop(sb.toString());

We can directly output the string

This sequential method is called the method call chain

Because of the characteristics of stringbuffer, we can insert the data inside, I now want to insert a string after a, how to implement it?

sb.append("abc").append(36);sb.insert(1"lgl");sop(sb.toString());

That's right. Insert, his two parameters, one is subscript, one is data, so we insert successfully

Let's talk about it again. Delete

    /**     * 删除     */    publicstaticvoidmethod_delete() {        new StringBuffer("abcdefg");        sop(sb.toString());        // 删除bc        // sop(sb.delete(1, 3).toString());        // 删除d        sop(sb.deleteCharAt(3));        // 清空缓冲区        sop("all:" + sb.delete(0, sb.length()));    }

In fact, these are relatively simple

OK, in order we are now talking about getting, in fact, we have already talked about the string, there is not much to say. We say modify, modify is more classic, modify the data we write this way

    /**     * 修改     */    publicstaticvoidmethod_update() {        new StringBuffer("abcdefg");        // 替换一部分        sop(sb.replace(14"java"));        // 替换一个        1‘k‘);        sop(sb.toString());    }

Results

OK, the modification succeeds, and the buffer is stored in the array

/** * Store data in the buffer in the array * /     Public Static void Method_getchar() {StringBuffer SB =NewStringBuffer ("ABCDEFG");Char[] CHS =New Char[4];/** * Starting from 1, 4 ends, exists in CHS, starts from 1 * /Sb.getchars (1,4, CHS,1); for(inti =0; i < chs.length; i++) {SOP ("char["+ i +"] = "+ Chs[i] +";"); }    }

Output of the results, hehe

Two. StringBuilder

It's only after JDK1.5.

    • StringBuffer: Thread Synchronization
    • StringBuilder: Thread not synchronizing

StringBuilder is not recommended in development

Let's take a look at his API description:

The use of the same, not much to say, this article is free to here

Interested in Dabigatran: 555974449

Java Tour (17)--stringbuffer Overview, store, delete, get, modify, invert, store buffer data into an array, StringBuilder

Related Article

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.