4

I have a string 1122333344555566778888 I need to substring it and as a result get [11, 22, 3333, 44, 5555, 66, 77, 8888] Is it possible to do it in a beautiful way or I need to hardcode it and eight time use string.substring(beginning, ending) function and then put into array?

Edit: String can contain not only repeated numbers. AB CG HERD KJ 98HQ 0K 1E OOQW it also example!

8
  • What have you tried so far? It is possible to do the splitting by arbitratry numbers. Commented Nov 17, 2012 at 12:31
  • Are the groups always numbers and in order? Commented Nov 17, 2012 at 12:32
  • 1
    What is the logic behind the splitting? Do you want to separate different numbers, or what is the goal? Commented Nov 17, 2012 at 12:32
  • I tried to hardcode it and eight time use string.substring(beginning, ending) function, but the code looks like terrible. As a result, I am looking for a beautiful solution. Commented Nov 17, 2012 at 12:34
  • 1
    Do you want to split based on A) predetermined lengths of the parts, regardless of content, or B) contiguous same-character blocks regardless of length? Commented Nov 17, 2012 at 13:38

7 Answers 7

4

use the pattern: ((\d)\2*)

String input = "1122333344555566778888";
Pattern p = Pattern.compile("((\\d)\\2*)");
Matcher m = p.matcher(input);
while (m.find()) {
    System.out.println("Found " + m.group(1));
}

produces:

11
22
3333
44
5555
66
77
8888

edit: if its numbers as well as spaces and letters use the pattern (([\d\w\s])\2*)

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

4 Comments

Pattern p = Pattern.compile("(([\d\w\s]\2*)"); gives an error in eclipse - invalid escape sequence
in java you need to escape the escape so you would need to use (([\\d\\w\\s]\\2*) to make it valid @Bob
Another error too: Unclosed group near index 14 (([\d\w\s]\2*)
sorry that was my mistake, i missed off a closing bracket ) updated answer to use (([\d\w\s])\2*) @Bob
3

You can use the regular expression of repeating characters:

String input = "1122333344555566778888";
String regex = "(\\w)\\1+";

Matcher m = Pattern.compile(regex).matcher(input);
String[] substrings = new String[input.length()];

int index = 0;

while (m.find())
    substrings[index++] = m.group();

for (int i = 0; i < index; i++)
    System.out.println(substrings[i]);

Output:

11
22
3333
44
5555
66
77
8888

Important Note:

substrings array contains null entries since its length is equal to the input string's length. If your string contains non-repeating sequential characters then this array might not have null entries. Watch on substrings for NullPointerException.

Comments

0

there is no delimiter in this string to be able to use .split(), if you had a delimeter between what you want to have a substring such as 11-22-3333- ... etc, it will be easy to use

String[] splits = asseltClasses.split("-");

1 Comment

I do not have any delimiters here, just plain string
0

Based on BlueBullet's ...

import java.util.regex.*;
import java.util.*;
public class MyTest {

    public static void main(String[] args) {

        String input = "1122333344555566778888";
        String regex = "(\\w)\\1+";

        Matcher m = Pattern.compile(regex).matcher(input);

        List<String> l = new ArrayList<String>();
        while (m.find()) l.add(m.group());

        System.out.println(Arrays.toString(l.toArray()));
    }   
}

Output:

[11, 22, 3333, 44, 5555, 66, 77, 8888]

Comments

0

This beautiful enough? It's just one line...

String parts = input.replaceAll("(.)(?!\\1)", "$1\0").split("\0");

Here's a test:

public static void main(String[] args) {
    String input = "1122333344555566778888";
    String[] parts = input.replaceAll("(.)(?!\\1)", "$1\0").split("\0");
    System.out.println(Arrays.toString(parts));
}

Output:

[11, 22, 3333, 44, 5555, 66, 77, 8888]

Note that there is one very minor hitch with this solution - the character after $1 in the call to replaceAll() must not be possible to have in the input. I chose the null character '\0' (ie hex zero) to be fairly safe.

Comments

0

This is what I would do: Given a string that is not sorted, "ABCABCABC", you can sort it by converting it to a Char[] array, then using Arrays.sort(), turning it into AAABBBCCC.

    public String[] sortThis(String inputData) {
    String input = "ABCABCABC"; //make this whatever you want (or set to inputData)
    String[] temp = new String[input.length()];
    for (int i = 0; i < input.length();i++) //initialize the array, or it prints "null"
        temp[i] = "";
    int index = 0;
    char[] info = input.toCharArray();
    Arrays.sort(info);

    for (int i = 0; i < input.length(); i++) { // fill the temp array
        temp[index] += info[i];
        if(i+1 < input.length())
            if(i < input.length() && info[i] != info[i+1])
                index++;
    }

    String[] answer = new String[index+1]; 
    for(int i = 0; i < index+1; i++) // shorten the array
        answer[i] = temp[i];

    return answer;
    }

Output:

    [AAA, BBB, CCC]

Comments

0

Maybe u want to use this:

var str="1122333344555566778888"; //whatever
b=0;outpt="";
while(b<=18){
if(b==4|b==10|b==18){e=4;p=4}else{e=2;p=2}
outpt+=str.substr(b,e)+", ";b+=p;}
alert(outpt);

Output:

11, 22, 3333, 44, 5555, 66, 77, 8888, 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.