0

Let's say I have a String[] that looks like this:

private String[] motherOfStrings = {
  "stringA",
  "stringB",
  "stringC",
  "stringD",
  "stringE",
}

Can I split it into multiple String[] like these?

private String[] childOfString1 = {
  "stringA",
  "stringB",
  "stringC",
}

private String[] childOfString2 = {
  "stringD",
  "stringE",
}

Thanks guys :)

p.s., i did some search but most of the guides (if not all) are about splitting String[] into Strings, not another String[]. Is this even possible?

1
  • I added more details on the original post :) Commented Apr 26, 2012 at 7:17

2 Answers 2

3

You can use split() method for every string in your array.

String stringByWhichYouWantToSplit = "C";
String[][] splittedStrings = new String[motherOfStrings][];
for(int i = 0; i < motherOfStrings.length; i++)
    splittedStrings[i] = motherOfStrings.split(stringByWhichYouWantToSplit);

...if you want to split your strings by "C" ...

EDIT:

Now, when you edited question I see what you want. You have to create two arrays and copy into them strings from motherOfStrings. You can use System.arraycopy method.

Sign up to request clarification or add additional context in comments.

Comments

0

Loop through array and split each String into String[] or better maintain a List<String>

Comments

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.