1

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

2
  • 1
    You can merge the arrays into one array later on. Commented Nov 29, 2012 at 8:09
  • @RohitJain Can you please provide an example? also not sure if this affects your answer but the data is not always set. That is how the data looks like but sometimes there might be 40 components to an array opposed to just 2. Thanks Commented Nov 29, 2012 at 8:11

6 Answers 6

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

I am new to java so do you mind explaining/writing the code exactly as it should be? Like where should I put testStrngArray? Thanks
wherever you already have your testStringArray you can call String[] newArray = splitContent("\n", testStringArray); and in that place you'll have the newArray containing the content you wanted.
Makes sense. Thanks. But what is the parameter "String...input"? Is that just String input? Thanks
Thanks, I added one more hint anyway.
0
    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()]);

Comments

0

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()]);

1 Comment

The only issue is it is hard coded... I don't always know what the exact data is/ how much data there will be. The above is how it will look but not necessarily what the data is. Any other suggestions? Thanks
0

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");

Comments

0

Try this. It is simple and readable.

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

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

Comments

-1

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));

2 Comments

No disrespect but you can certianly have this as a string within a string array "Mark,36"
Thats correct. Though, judging from the post there was not quotation-marks around the String, hence the comma-mark would indicate that the he is trying to initialize 2 String (or in my opinion 1 String and 1 Integer) in the String-array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.