CuriousCoder
CuriousCoder

Reputation: 1600

Splitting a string java based on multiple delimiters

I need to split a string based on delimiters and assign it to an object. I am aware of the split function, but I am unable to figure how to do it for my particular string.

The object is of the format:

class Selections{
int n;
ArrayList<Integer> choices;
}

The string is of the form :

1:[1,3,2],2:[1],3:[4,3],4:[4,3]

where:

1:[1,3,2] is an object with n=1 and Arraylist should have numbers 1,2,3. 
2:[1] is an object with n=2 and Arraylist should have number 1

and so on .

I cannot use split with "," as delimiter because both individual objects and the elements within [] are separated by ",".

Any ideas would be appreciated.

Upvotes: 0

Views: 395

Answers (5)

gifpif
gifpif

Reputation: 4917

Try this

while(true){
        int tmp=str.indexOf("]")+1;
        System.out.println(str.substring(0,tmp));
        if(tmp==str.length())
            break;
        str=str.substring(tmp+1);   
    }

Upvotes: 0

Peter Lamby
Peter Lamby

Reputation: 295

I have no idea how to use a build-in function for this. I would just write my own split method:

private List<Sections> split(String s){
    private List<Sections> sections = new ArrayList<>();
    private boolean insideBracket = false;
    private int n = 0;
    private List<Integer> ints = new ArrayList<>();

    for (int i = 0; i < s.length(); i++){
        char c = s.charAt(i); 
        if(!insideBracket && !c.equals(':')){
            n = c.getNumericValue();
        } else if(c.equals('[')){
            insideBracket = true;
        } else if (c.equals(']')){
            insideBracket = false;
            sections.add(new Section(n, ints));
            ints = new ArrayList();
        } else if(insideBracket && !c.equals(',')){
            ints.add(c.getNumericValue());
        }
    }
}

you probably need to modify that a little bit. Right now it dont works if a number has multiple digits.

Upvotes: 0

Giovanni Botta
Giovanni Botta

Reputation: 9816

You could use a regex to have a more robust result as follows:

String s = "1:[1,3,2],2:[1],3:[4,3],4:[4,3],5:[123,53,1231],123:[54,98,434]";
// commented one handles white spaces correctly
//Pattern p = Pattern.compile("[\\d]*\\s*:\\s*\\[((\\d*)(\\s*|\\s*,\\s*))*\\]");
Pattern p = Pattern.compile("[\\d]*:\\[((\\d*)(|,))*\\]");
Matcher matcher = p.matcher(s);

while (matcher.find())
  System.out.println(matcher.group());

The regex can probably be tuned to be more accurate (e.g., handling white spaces) but it works fine on the example.

Upvotes: 1

DwB
DwB

Reputation: 38290

You will need to perform multiple splits.

  1. Split with the delimiter "]," (as mentioned in other comments and answers).
  2. For each of the resulting strings, split with the delimiter ":[".
  3. you will need to cleanup the last entry (from the split in step 1), because it will end with ']'

Upvotes: 0

Robin
Robin

Reputation: 99

How about using "]," as delimiter? If your structure is strictly like you said, it should be able to identify and split.

(Sorry, I want to leave it as comment, but my reputation does not allow)

Upvotes: 1

Related Questions