简体   繁体   中英

Split String and Store in Array using Java

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 an array of strings , I tried String.Split(), but I failed to catch the inner values.

Do you think regular expressions could help???

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

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

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("', '");
   String s1= "['Aaaa', 'Eeee', 'Ffff', 'Cccc', 'Dddd']";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM