3

I have the following string and would like to convert it to string array

String singleArray = "[501]"
String multipleArray = "[1,501,634]"

I would like to get List<String> from that, the thing is sometimes it can be a single value and sometimes it can be multiple value seperate by comma.

I tried to use Arrays.asList(multipleArray) but it didn't work.

Any suggestions?

1
  • Have you tried something using String.split? Careful it takes a regex as an argument. Commented Apr 7, 2015 at 8:15

5 Answers 5

9
  1. Get the substring between the first and the last character in order to get rid of the [ and ] characters.
  2. Split the resulting string by ,.

For example:

String input = "[1,501,634]";
String[] result = input.substring(1, input.length() - 1).split(",");
List<String> asList = Arrays.asList(result);
Sign up to request clarification or add additional context in comments.

Comments

7

How about

String arr [] = str.replace("[", "").replace ("]", "").split (",");

And then as per your knowledge, you can use the array to create a List using

Arrays.asList(arr);

Note

As noted by Bas.E, my solution works upon the data as per your example.

4 Comments

What about String str = "[1, '[501]', 634]" ? The OP never stated that strings would be Integer only, so your method could create an invalid output
Well...the answer(s) are based on the info provided in the question, @Bas.E. :)
But how often don't we write questions with mockup data? What if one copies this answer into production code and the system will actually take string arrays as input? Remember that the OP will not be the only one seeing (and using) this answer
@Bas.E Thanks I have added a note to my answer.
1

Remove the leading and trailing square brackets:

String removedBrackets = multipleArray.replaceAll( "^\\[|\\]$", "" );

or

String removedBrackets = multipleArray.substring( 1, multipleArray.length() - 1 );

Then split the string on the commas:

String[] arrayOfValues = removedBrackets.split( "," );

Then convert it to a list:

List<String> listOfValues = Arrays.asList( arrayOfValues );

Comments

1
  1. Remove [ and ] from the beginning and ending using substring.
  2. split rest of the string according to the occurrence of ,.

    String[] arr = multipleArray.substring( 
                        1, multipleArray.length()-1).split(",");
    
  3. then use the array to make a list.

    List<String> list=Arrays.asList(arr);
    

4 Comments

Please add some words why you think that this is a solution.
i don't know why its necessary, yet added some explanation. @Trinimon
your answer was marked as low quality post due to its short length. Personally I think it's not bad to have some explanation, even if the solution seems to be simple or obvious. Thanks!
i didn't know that low quality issue. Thank you very much. I will try to explain as best as i can, thank you again.@Trinimon
0

trim it from side brackets

array = array.substring(1,array.length()-1);

then split and convert to array

String[] arrayStringArray = array.split(",");

and if wanted, make it a List

List<String> arrayStringList = Arrays.asList(arrayStringArray);

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.