Alex G
Alex G

Reputation: 2309

Java split string from array

I have a string array that contains some information.

Example:

 String [] testStringArray;

 testStringArray[0]= Jim,35
                     Alex,45 
                     Mark,21

 testStringArray[1]= Ana,18
                    Megan,44

This is exactly how the information is. Now my problem is I want to make each element a seperate element in an array and I want to split it based on the \n character.

So I want

        newArray[0]=Jim,35
        newArray[1]=Alex,45
        newArray[2]=Mark,21
        newArray[3]=Ana,18

etc etc. I am aware of the split method but won't this just split each array element into a completely new array instead of combining them?

If anyone could help, it would be appreciated. Thanks

Upvotes: 1

Views: 1654

Answers (6)

jlordo
jlordo

Reputation: 37813

Something like this:

    // Splits the given array of Strings on the given regex and returns
    // the result in a single array.
    public static String[] splitContent(String regex, String... input) {
        List<String> list = new ArrayList<>();
        for (String str : input) {
            for (String split : str.split(regex)) {
                list.add(split);
            }
        }
        return list.toArray(new String[list.size()]);
    }

you can call it this way:

    String[] testStringArray = ...;
    String[] newArray = splitContent("\n", testStringArray);

Because of the use of varargs you can also call it like this:

    String[] newArray = splitContent("\n", str1, str2, str3, str4);

where strX are String variables. You can use any amount you want. So either pass an array of Strings, or any amount of Strings you like.

If you don't need the old array anymore, you can also use it like this:

    String[] yourArray = ...;
    yourArray = splitContent("\n", yourArray);

Upvotes: 2

Flexo
Flexo

Reputation: 2556

Try this. It is simple and readable.

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

for (String s : testStringArray) {
    newArray.addAll(Arrays.asList(s.split("\\n"));
}

Upvotes: 0

Wang Wei
Wang Wei

Reputation: 583

you can first merge the strings into one string and then use the split method for the merged string.

testStringArray[0]= Jim,35
                     Alex,45 
                     Mark,21

testStringArray[1]= Ana,18
                    Megan,44

StringBuffer sb = new StringBuffer();

for(String s : testStringArray){

    s = s.trim();
    sb.append(s);
    if (!s.endWith("\n")){
        sb.append("\n");
    }

}

String[] array = sb.toString().split("\n");

Upvotes: 0

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

Try This is working Code >>

String[] testStringArray = new String[2]; // size of array

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

    testStringArray[0]= "Jim,35\nAlex,45\nMark,21"; // store value
    testStringArray[1]= "Ana,18\nMegan,44";

    for(String s : testStringArray) {
        String[] temp = s.split("\n"); // split from \n
        for(String t : temp) {
            result.add(t);   // add value in result
            System.out.print(t);


        }
    }
    result.toArray(new String[result.size()]);

Upvotes: 0

chribsen
chribsen

Reputation: 6620

Firstly, you can't write what you just did. You made a String array, which can only contain Strings. Furthermore the String has to be in markers "" like "some text here".

Furthermore, there can only be ONE String at one place in the array like:

 newArray[0] = "Jim";
newArray[1] = "Alex";

And NOT like:

newArray[0] = Jim;

And CERTAINLY NOT like:

// Here you're trying to put 2 things in 1 place in the array-index
newArray[0] = Jim, 35;

If you wan't to combine 2 things, like an name and age you have to use 2D array - or probably better in your case ArrayList.

Make a new class with following object:

  public class Person {

    String name;
    int age;

    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
    }

And afterwards go to your class where you want to use the original array, and write:

ArrayList<Person> someNameOfTheArrayList = new ArrayList<Person>();

someNameOfTheArrayList.add(new Person("Jim", 32));
someNameOfTheArrayList.add(new Person("Alex", 22));

Upvotes: -1

lawl0r
lawl0r

Reputation: 870

    String[] testStringArray = new String[2];
    ArrayList<String> result = new ArrayList<String>();
    testStringArray[0]= "Jim,35\nAlex,45\nMark,21";
    testStringArray[1]= "Jiam,35\nAleax,45\nMarak,21";
    for(String s : testStringArray) {
        String[] temp = s.split("\n");
        for(String t : temp) {
            result.add(t);
        }
    }
    String[] res = result.toArray(new String[result.size()]);

Upvotes: 0

Related Questions