0

How would I go about splitting a String in several places where ever a certain character occurs.

For example, here is the String

"10067829311288"

When looping through the String, if the digit is a "1", it will take the next the next two characters and split, if not, it will take only one. Here's how it should be split.

100 67 82 93 112 88

Any help is appreciated.

1
  • How come the double 1 results in 112 instead of 1128? Commented Jun 1, 2013 at 15:25

3 Answers 3

3

How about something like this:

public static void main(String[] args) {
    final String s = "10067829311288";
    final List<Integer> split = new LinkedList<>();
    int i = 0;
    while (i < s.length()) {
        final char c = s.charAt(i);
        final int remain = s.length() - i;
        final int step;
        if (c == '1') {
            step = Math.min(remain, 3);
        } else {
            step = Math.min(remain, 2);
        }
        split.add(Integer.parseInt(s.substring(i, i + step)));
        i += step;
    }
    System.out.println(split);
}

Or, with regex:

public static void main(String[] args) {
    final String s = "10067829311288";
    final List<Integer> split = new LinkedList<>();
    final Pattern pattern = Pattern.compile("1?\\d{2}");
    final Matcher matcher = pattern.matcher(s);
    while (matcher.find()) {
        split.add(Integer.parseInt(matcher.group()));
    }
    System.out.println(split);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the examples, I couldn't get my head around how to do the "step" part. Can you also explain what this regex does "1?\\d{2}" please?
the regex 1?\\d{2} matches an optional 1 greedily followed by two digits. So if there isn't a 1 it will grab two digits, if there is a one it will grab that and two more digits.
2

Here is one possible solution.

public void split() {
    System.out.println(split("10067829311288"));
}

public List<String> split(String text) {
    List<String> result = new LinkedList<>();

    Matcher m = Pattern.compile("1?\\d\\d").matcher(text);

    while (m.find()) {
        result.add(m.group());
    }
    return result;
}

Update: simplified my regex after seeing how Boris the Spider did his regex.

1 Comment

Thanks, I'm guessing \\d{2} is the same as \\d\\d?
0

You can try this way

"10067829311288".split("(?<=\\G(1\\d\\d|[02-9]\\d))");

demo

System.out.println(Arrays.toString("10067829311288".split("(?<=\\G(1\\d\\d|[02-9]\\d))")));

output: [100, 67, 82, 93, 112, 88]


\\G represents position of last match (split) and if first match doesn't exists it represents start of the String.

(?<=regex) is look-behind mechanism

\\d represents digit

| is OR

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.