0

I want to convert this String:

String s = "[\"val1\", \"val2\", \"val3\"]";

to an array or an ArrayList (without regex and split). It's from a Database, and it's generic. It's also possible to have commas inside the high quotes.

Is this possible?

9
  • Hello, did you try regex ? Commented Dec 11, 2018 at 10:52
  • yes, I have a solution with regex. My question is if there is an easy solution to do this in java? @Mickael Commented Dec 11, 2018 at 10:54
  • why do you ask if it's possible if you already have a solution.. Commented Dec 11, 2018 at 10:54
  • because it's about if there is a better solution than parsing it with regex and creating an array. Maybe there is a good library or a method. It looks like String[] toString... (same format) Commented Dec 11, 2018 at 10:56
  • it's about if there is a better solution than parsing it with regex and creating an array - and we are supposed to guess this when you ask "is this possible"? Commented Dec 11, 2018 at 10:58

4 Answers 4

1

Unfortunately, there is no java API that would directly parse this list.

This snippet would work for this String:

    String s = "[\"val1\", \"val2\", \"val3\"]";
    String[] arr = s.replace("[", "").replace("]", "").split("\\s*,\\s*");
    List<String> list = Arrays.asList(arr);

If the string contains [ or ] somewhere else, then the snippet above won't work.

I suggest to figure out what is this. Is this a JSON array? Then use a JSON parser. Is this a csv with additional [ and ]? Then use a CSV parser.

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

1 Comment

Is this a JSON array? - it is
1
String json = "[\"val1\", \"val2\", \"val3\"]";
JSONArray jsonarray = new JSONArray(json);
List<Object> objects = jsonarray.toList();

Thanks to @TamasRev

Comments

1

Yes, there is a way using Regex.

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class T1 {
    public static void main(String[] args) {
        final String REGEX = "\\b([\\w]+)\\b";
        String INPUT = "[\"val1\", \"val2\", \"val3\"]";

        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(INPUT);   // get a matcher object
        List<String> myList = new ArrayList<>();

        while(m.find()) {
            myList.add(m.group());
        }

        System.out.println(myList);
    }
}

Comments

0

You can use a String tokenizer and set as delimiters " \",[]". This is my example. I've used an array, but it's very simple to convert it in a ArrayList if you want.

public static void main(String[] args) {
    String s = "[\"val1\", \"val2\", \"val3\"]";

    String delims = " \",[]";

    StringTokenizer st = new StringTokenizer(s, delims);
    
    System.out.println("Num of Token = " + st.countTokens());
    String[] myArr= new String[st.countTokens()];
    
    
    for(int i=0; i<myArr.length && st.hasMoreTokens(); i++)
        myArr[i]=st.nextToken();
    
    //output
    System.out.println(Arrays.toString(myArr));
}

Your output will be:

Num of Token = 3

[val1, val2, val3]

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.