Split String and Store in Array using Java

46,091

Solution 1

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

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

Solution 2

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("', '");

Solution 3

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

       String[]  array = s1.split(",");
       for(int i=0;i<array.length;i++)
       {
           System.out.println(array[i]);
       }
Share:
46,091
user1899713
Author by

user1899713

Updated on July 09, 2022

Comments

  • user1899713
    user1899713 almost 2 years

    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???