Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java string concatenation techniques (StringBuilder tips)     - Ubuntu 64-bit installation Adobe Reader 9.5.5 (Linux)

- Getting Started with Linux system to learn: how to check memory usage of Linux (Linux)

- Configuration OpenOCD + FT2232 under Ubuntu (Linux)

- iOS custom keyboard emoji expression (Programming)

- hadoop 2.7.1 High Availability Setup Deployment (Server)

- About Auto Layout and frame (Programming)

- expdp reported ORA-39181 Export Processing Method (Database)

- Win8 mount disk partitions under Ubuntu (Linux)

- ORA-00824: can not set sga_target due to existing problem-solving (Database)

- Linux with Windows Explorer as a security system (Linux)

- Plasma 5.4 How to install on Kubuntu 15.04 (Linux)

- Configuring ftp server and nfs server under Linux (Server)

- Linux Command Study Notes (Linux)

- Log4j configuration file Explanation (Linux)

- Using Linux stat command to view the files (Linux)

- MySQL backup tool to back up mydumper (Database)

- Oracle inline view updates problems encountered (Database)

- Oracle 11g maintenance partitions (Seven) - Modifying Real Attributes of Partitions (Database)

- Oriented C ++ test-driven development (Programming)

- Linux Getting Started tutorial: Borrow Windows fonts in Ubuntu 14.10 (Linux)

 
         
  Java string concatenation techniques (StringBuilder tips)
     
  Add Date : 2018-11-21      
         
       
         
  In normal development, we need to splice strings may encounter the following format (at least I met many times):

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

This feature string: divided between a plurality of data by certain special symbols.

Before I do think this way, I believe that a lot of people think so:

public class StringTest

{

    public static void main (String [] args)

    {

        StringBuilder sb = new StringBuilder ();

        for (int i = 0; i <= 10; i ++)

        {

// This judgment for the following people who have obsessive-compulsive disorder is really a bit uncomfortable, each cycle must come to judge the following, if the number of cycles a lot of it, we will think it will not affect performance?

            if (i! = 0)

            {

                sb.append ( ",");

            }

            sb.append (i);

        }

        System.out.println (sb.toString ());

    }

}


See here, you is not the usual practice is such that each cycle there is such a judgment, and if the answer is yes, then you have a need to continue to look down, because more than one way really not very wise . (At least for me this && obsessive pursuit of perfect people, this code makes me very uncomfortable).

Here no one to refute the code I used to "+" string concatenation to it, if you really think so, Oh My God !!!

I am here I know two ways to solve the problems of this phenomenon:

Method One: First, remove the first element of the collection, by the StringBuilder constructor, passing the first element, this is not the end of each cycle time do not need to judge ah?

public class StringTest

{

    public static void main (String [] args)

    {

        StringBuilder sb = new StringBuilder ( "0");

        for (int i = 1; i <= 10; i ++)

        {

            sb.append ( ",");

            sb.append (i);

        }

        System.out.println (sb.toString ());

    }

}

Many small partners think the above is through skills (proficiency java api master's) will first take out a special element, special treatment, the latter is not every element has a law, and are based on a character plus previous data. Thought of this, you have not thought of something else?

As a programmer we should be trained in a skill of replication, since the above manner can be the first element as a special separate data extracted, then there is no way to final data also serves as a special separate data extracted from the front other data is not there a pattern yet?

So here it brings me to my second solution ( "Java programming ideas, the fourth edition of" P286)

Second way: Many people are familiar with the StringBuilder append (); method, but many people do not know the StringBuilder delete (int startPost, int endPost); if you have a habit of giving top priority to the study, it can easily grasp the interface to the api . In fact, many are opposed in this way to learn in the java api, and soon you will find yourself on a lot to know.

public class StringTest

{

    public static void main (String [] args)

    {

        StringBuilder sb = new StringBuilder ();

        for (int i = 0; i <= 10; i ++)

        {

            sb.append (i);

            sb.append ( ",");

        }

        sb.delete (sb.length () - 1, sb.length ()); // here to see if you know the api interface to the

        System.out.println (sb.toString ());

    }

}

I tell you more than just a little knowledge of this technique for point in learning more should learn by analogy, you learn to master the learning method of programming a shortcut. Welcome to a better way of learning, learning skills, knowledge and skills to discuss and share with me.
     
         
       
         
  More:      
 
- Configure the Linux kernel and use iptables to do port mapping (Linux)
- Summary of Docker mounted directory (Server)
- VMware6 achieve nat Internet (Linux)
- Ora-00600 [fast hot Atkins soft _ that _ Oh, of course not _less_ profile] (Database)
- Locale files under Ubuntu (Linux)
- Haproxy multi-domain certificate HTTPS (Server)
- Install Java, Maven, Tomcat under Linux (Linux)
- Linux Firewall Builder shorewall (Linux)
- Incremental garbage collection mechanism for Ruby 2.2 (Programming)
- numpy and SciPy installation under Python for scientific computing package (Linux)
- Netfilter / Iptables Comments (Linux)
- Docker + OpenvSwitch build experimental environment VxLAN (Server)
- How to install CentOS CentOS web page (Server)
- CentOS 7 server environment to quickly build Linux (Server)
- GAMIT baseline solution using batch were counted (Linux)
- Linux C source code (Ascii HexToBinary: Converts hexadecimal string format ASCII codes) (Programming)
- floating IP in OpenStack neutron (Server)
- MySQL optimization tabs (Database)
- grep command output highlighted word (Linux)
- Linux, set and view environment variables (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.