Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java string concatenation techniques (StringBuilder tips)     - MySQL 5.6 database code changes (Database)

- How to add two-factor authentication for Linux systems SSH (Linux)

- Nginx Performance Tuning Guidelines (Server)

- Windows Desktop use VNC remote connect Linux (Linux)

- Repair Maven project developed default Maven Plugin folder (Linux)

- Linux operating system log system (Linux)

- Use window.name + iframe cross-domain access to data Detailed (Programming)

- Installation and Configuration OpenVPN server and client on Ubuntu 15.04 (Server)

- What is Unikernel? (Linux)

- linux smartd [FAILED] appears at startup (Linux)

- Ant command-line compiler Android project (Programming)

- Linux platform host to prevent hacking skills (Linux)

- Object Oriented Programming Java reflection (Programming)

- Copy Recovery using RMAN repository development environment (Database)

- To install Ganglia (Linux)

- Python regular expressions: how to use regular expressions (Programming)

- Linux System Getting Started Learning: Linux command in w (Linux)

- Use virt-manager to create and manage virtual machines (Linux)

- Linux Getting Started tutorial: 3D effects and beautify your desktop (Linux)

- AngularJS - Custom instructions (Programming)

 
         
  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:      
 
- Ubuntu to install systems Indicator Sticky Notes 0.4.4 (Linux)
- The simple multi-threaded Python (Programming)
- Android 5.1 OTA package compilation error (Programming)
- Vim plugin installation YouCompleteMe (Linux)
- Linux regex sed detailing (Linux)
- Linux installed xdotool simulate keystrokes and mouse movements (Linux)
- CentOS 7 install Hadoop-cdh-2.5 on Mesos (Server)
- Struts2 form of non-use component tags (Programming)
- Use Epoll develop high-performance application server on Linux (Server)
- 12 kinds of detection of CPU information on a Linux system tools (Linux)
- Ubuntu 14.04 Fixed update information is outdated error (Linux)
- Inherent limitations of Linux systems network security (Linux)
- Virtualbox virtual machine can not copy CentOS Network (Linux)
- Oracle database NUMBER (x, y) data types (Database)
- Physical structure and process disk IO (Linux)
- swap space is insufficient cause OOM kill MySQL Case (Database)
- First start with Kali Linux 2.0 (Linux)
- How do you access Dropbox Linux command line (Linux)
- Ubuntu users to install Gnome-Pie 0.6.5 (Linux)
- JDK comes with tools jinfo (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.