Java String stitching tips (StringBuilder tips for use)

Source: Internet
Author: User

In peacetime development, we may encounter strings that need to be spliced in the following format (at least I have encountered many times):

1,2,3,4,5,6,7,8,9,10,11,12,12,12,12,34,234,2134,1234,1324,1234,123

The character of this string: Multiple data is separated by a special symbol.

I used to think so, and I believe a lot of people are thinking the same way:

public class stringtest{public    static void Main (string[] args)    {        StringBuilder sb = new StringBuilder (); 
   for (int i = 0; I <=; i++)        {//The following judgment is a bit uncomfortable for people with obsessive-compulsive disorder, every time the loop comes in to judge the following, if the number of cycles is many, we will think will not affect the performance?            if (i! = 0)            {                sb.append (",");            }            Sb.append (i);        }        System.out.println (Sb.tostring ());}    }

See here, your usual practice is not the same, every cycle there is such a judgment, if the answer is affirmative, then you need to continue to look down, because the above way is not very wise. (At least for my obsessive-compulsive && pursuit of perfection, this code makes me uncomfortable).

There is no one here to refute the code, let me use "+" to stitch the string bar, if you really think so, Oh My God!!!

I am here to provide two kinds of ways I know to solve this phenomenon problem:

Method One: First take out the first element of the collection, through the construction method of the StringBuilder, the first element, so that is not the back of every loop does not need to judge Ah?

public class stringtest{public    static void Main (string[] args)    {        StringBuilder sb = new StringBuilder ("0"); for        (int i = 1; i <=; i++)        {            sb.append (",");            Sb.append (i);        }        System.out.println (Sb.tostring ());}    }

Many small partners think of the way above is through the skill (Java API Mastery) to the first special element to take out, special processing, the following is not every element has a regular, all is a character plus a data. Think of this place, did you think of something else?

As a programmer we should be able to acquire a extrapolate, the above way since the first element as a special data can be extracted separately, then there is no way to the last data also as a special data extracted separately, the other data in front of it is not the law can follow it?

So here's my second solution (Java Programming thought Fourth edition P286)

Way two: Many people are familiar with StringBuilder's append () method, but many people do not know StringBuilder's Delete (int startpost,int endpost); If you have extrapolate learning habits, Then it's easy to master the API interface. In fact, many of the Java API is relative, in this way to learn, and soon you will find that you know a lot.

public class stringtest{public    static void Main (string[] args)    {        StringBuilder sb = new StringBuilder (); 
   for (int i = 0; I <=; i++)        {            sb.append (i);            Sb.append (",");        }        Sb.delete (Sb.length ()-1, Sb.length ());//Here you know this API interface is        SYSTEM.OUT.PRINTLN (sb.tostring ());}    }

  

The above is just a small knowledge of my skills to tell you that in the study should learn more extrapolate, Master learning method is you learn programming a shortcut. You are welcome to have a better learning method, learning skills, knowledge points and skills to discuss and share with me.

Reprint Please specify source: http://www.cnblogs.com/liushaofeng89/p/4865263.html

Java String stitching tips (StringBuilder tips for use)

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.