3

I have a string that would always output in the following format, but with different lengths.

String s1= "['Aaaa', 'Eeee', 'Ffff', 'Cccc', 'Dddd']";

How to split this string and store the values in a n array of strings, I tried String.Split(), but I failed to catch the inner values.

Do you think regular expressions could help???

8
  • Why not use a JSON parser as you should? Commented Dec 13, 2012 at 8:05
  • 2
    String.Split() it does not exist. Can you post your code so we make sure you really tried something? Commented Dec 13, 2012 at 8:06
  • what is your String ?? is s1 and s2 your original strings and you wanna split them such that your resulting array is {Aaaa, Eeee.....}?? Commented Dec 13, 2012 at 8:06
  • @NikolayKuznetsov Perhaps String#split(String) was meant here Commented Dec 13, 2012 at 8:07
  • These look JSON encoded - why not just use a JSON decoder which will turn them into an array? Commented Dec 13, 2012 at 8:07

4 Answers 4

5

Have you tried running it through Split with the regex parameter as"[\\[\\]\\', ]"

Basically any of [,],,,', - more information here

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

Comments

4

This is my code , Hope it will help

    String s1 = "['Aaaa', 'Eeee', 'Ffff', 'Cccc', 'Dddd']";
    String s2 = "['Aaaa', 'Eeee']";
    String[] xx = s1.substring(2, s1.length() - 2).split("', '");

Comments

1
   String s1= "['Aaaa', 'Eeee', 'Ffff', 'Cccc', 'Dddd']";

       String[]  array = s1.split(",");
       for(int i=0;i<array.length;i++)
       {
           System.out.println(array[i]);
       }

2 Comments

Close. What about the brackets?
Can be replaced. i write for the split only. Thanks for correction.
1

Suppose

 String s = "dog cat cat dog";

//Now you want to split this String and save in to String[] (String Array)

String[] t= s.split(" ");

System.out.println(Arrays.toString(t));

// output you will get as ["dog", "cat", "cat", "dog"];

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.