Java with delimited strings, string arrays, and conversions between arraylist<string>

Source: Internet
Author: User

Java with delimited strings, string arrays, and conversions between arraylist<string>

The beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or re-reproduced, thank you for your cooperation.


First, to meet the title of the three things, perhaps the description is not clear, but with the following example, there will be no ambiguity

1, delimited string is this:

    String seperatestr = "111,222,333,444,555,666,777";
Separated by commas, or separated by other characters or strings, or with multiple separators;


2. Array of strings:

    String[] Seperates = {"111", "222", "333", "444", "555", "666", "777"};

can also

    String seperates[] = {"111", "222", "333", "444", "555", "666", "777"};

Or

    int arraySize = 7;    string[] seperates = new String[arraysize];    Seperates[0] = "111";    Seperates[0] = "222";    Seperates[0] = "333";    Seperates[0] = "444";    Seperates[0] = "555";    Seperates[0] = "666";    Seperates[0] = "777";
Again or

    string[] seperates = new string[]{"111", "222", "333", "444", "555", "666", "777"};

3, arraylist<string>

    List seperatelist = new arraylist<string> ();

Second, how to convert between three things?

1. Converting between string arrays with delimited strings

A, traversing stitching, converting from a string array to a delimited string;

b, check the number of separators to initialize the string array, and then check the separation location to the substring assigned to the string array elements;


The above mentioned two methods are really very low-level, but it is really a waste of time, why Java's built-in methods are not as easy to handle in IOS:

        NSString *seperatestr = @ "111,222,333,444,555,666,777";        Nsarray *seperates = [Seperatestr componentsseparatedbystring:@ ","];                NSString *seperatebackstr = [seperates componentsjoinedbystring:@ ","];

Don't worry, Java does not bring your own, may wish to find foreign aid.

Apache Commons Lang

This is the generic tool library provided by Apache and the detailed documentation on the string tool StringUtils is available on this page.

    string[] seperates = Stringutils.split (Seperatestr, ",");    String resultstr = Stringutils.join (Seperates, ",");
That's all there is! The only regret, the function is not cohesive, need to introduce a three-party tool library.

In fact, the Java String class provides the Split method, which can be separated into groups, but is stretched to the advantage of the above library:


The String class provides a separation method:


StringUtils provides the separation method:



This tool library functions much more than this, the following summary section:

2. Conversion between string array and arraylist<string>

This is done using the collections class method to complete the conversion.


Adds an element from a string array to the arraylist<string> once:

    Collections.addall (Seperatelist, seperates);

Convert arraylist<string> to an array:

    String[] Seperatestr = (string[]) Seperatelist.toarray ();

3, as for the conversion between the arraylist<string> and the string, may have a different meaning

If you want to press the above separation, then there is the above chain, enough to convert, and indeed there is no easy way;

Again, the ToString () method renders the ArrayList object as a string, describing it only, and you can derive subclasses, overload the ToString method, organize it by your own intent, or add a parameter, passing in the delimiter.




Java with delimited strings, string arrays, and conversions between arraylist<string>

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.