How can I split a string into several parts?

I have a line with a few words separated by spaces, for example. "firstword second third" and ArrayList. I want to split a string into several parts and add a string of "pieces" to an ArrayList.

For example, "firstword second third" can be divided into three separate lines, so an ArrayList will have 3 elements; "1 2 3 4" can be split into 4 lines, into 4 ArrayList elements. See code below:

public void separateAndAdd(String notseparated) {
    for(int i=0;i<canBeSepartedinto(notseparated);i++{
    //what should i put here in order to split the string via spaces?
        thearray.add(separatedstring);
    }
}

public int canBeSeparatedinto(String string)
    //what do i put here to find out the amount of spaces inside the string? 
    return ....
}

Please leave a comment if you do not understand what I mean, or I have to fix some errors in this post. Thank you for your time!

+5
source share
6 answers

You can split the string into spaces using split():

String[] parts = inputString.split(" ");

( !"".equals(parts[i]) .

+9

, .split(" ");. , .split(" +");.

:

class SplitTest {
    public static void main(String...args) {
        String s = "This is a  test"; // note two spaces between 'a' and 'test'
        String[] a = s.split(" ");
        String[] b = s.split(" +");
        System.out.println("a: " + a.length);
        for(int i = 0; i < a.length; i++) {
            System.out.println("i " + a[i]);
        }
        System.out.println("b: " + b.length);
        for(int i = 0; i < b.length; i++) {
            System.out.println("i " + b[i]);
        }
    }
}

, "\\s+" " +", "\\s" , .

, :

void separateAndAdd(String raw) {
    String[] tokens = raw.split("\\s+");
    theArray.ensureCapacity(theArray.size() + tokens.length); // prevent unnecessary resizes
    for(String s : tokens) {
        theArray.add(s);
    }

}

- , separateAndAdd, , .

import java.util.*;
class SplitTest {
    public static void main(String...args) {
        SplitTest st = new SplitTest();
        st.separateAndAdd("This is a test");
        st.separateAndAdd("of the emergency");
        st.separateAndAdd("");
        st.separateAndAdd("broadcast system.");

        System.out.println(st);
    }

    ArrayList<String> theArray = new ArrayList<String>();

    void separateAndAdd(String raw) {
        String[] tokens = raw.split("\\s+");
        theArray.ensureCapacity(theArray.size() + tokens.length); // prevent unnecessary resizes
        for(String s : tokens) {
            if(!s.isEmpty()) theArray.add(s);
        }
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for(String s : theArray)
            sb.append(s).append(" ");
        return sb.toString().trim();
    }
}
+4

:

thearray = new ArrayList<String>(Arrays.asList(notseparated.split(" ")));

thearray

thearray.addAll(Arrays.asList(notseparated.split(" ")));
+2

apache.commons.lang.StringUtils.

, , .

split:

You can also refer to other parameters available for the split method using the same link.

+2
source

You can do it using .split()try this

String[] words= inputString.split("\\s");
+1
source

If you want to split the line in different parts, like here, I will show you how I can split this line on 03-14-2016 by day, month and year.

 String[] parts = myDate.split("-");
           day=parts[0];
           month=parts[1];
           year=parts[2];
+1
source

All Articles