5

There's a string

String str = "ggg;ggg;nnn;nnn;aaa;aaa;xxx;xxx;";

How do I split it into strings like this "ggg;ggg;" "nnn;nnn;" "aaa;aaa;" "xxx;xxx;" ???????

  • 1
    One approach would be to split it at every separator, and then re-combine the pairs. Another approach would be to write an explicit finite-state machine. – Solomon Slow Oct 08 '16 at 19:45

5 Answers5

3

Using Regex

    String input = "ggg;ggg;nnn;nnn;aaa;aaa;xxx;xxx;";
    Pattern p = Pattern.compile("([a-z]{3});\\1;");
    Matcher m = p.matcher(input);
    while (m.find())
        // m.group(0) is the result
        System.out.println(m.group(0));

Will output

ggg;ggg;

nnn;nnn;

aaa;aaa;

xxx;xxx;

Community
  • 1
  • 1
Erik
  • 221
  • 1
  • 7
  • That works for the specific pattern of 3 repeating characters followed by a semi-colon but won't work with any similar but slightly divergent patterns such as gg;ggg;n;nnnn; so the applicability of this concise solution for general use is rather limited. – beaudet Oct 25 '18 at 14:33
1

Split and join them.

 public static void main(String[] args) throws Exception {
        String data = "ggg;ggg;nnn;nnn;aaa;aaa;xxx;xxx;";
        String del = ";";
        int splitSize = 2;

        StringBuilder sb  = new StringBuilder();
        for (Iterable<String> iterable : Iterables.partition(Splitter.on(del).split(data), splitSize)) {
            sb.append("\"").append(Joiner.on(del).join(iterable)).append(";\"");
        }
        sb.delete(sb.length()-3, sb.length());
        System.out.println(sb.toString());

    }

Ref : Split a String at every 3rd comma in Java

Community
  • 1
  • 1
cody123
  • 2,040
  • 24
  • 29
1

I assume that the you only want to check if the last segment is similar and not every segment that has been read.

If that is not the case then you would probably have to use an ArrayList instead of a Stack.

I also assumed that each segment has the format /([a-z])\1\1/.

If that is not the case either then you should change the if statement with:

(stack.peek().substring(0,index).equals(temp))

public static Stack<String> splitString(String text, char split) {
    Stack<String> stack = new Stack<String>();
    int index = text.indexOf(split);
    while (index != -1) {
        String temp = text.substring(0, index);
        if (!stack.isEmpty()) {
            if (stack.peek().charAt(0) == temp.charAt(0)) {
                temp = stack.pop() + split + temp;
            }
        }
        stack.push(temp);
        text = text.substring(index + 1);
        index = text.indexOf(split);
    }
    return stack;
}
nick zoum
  • 7,216
  • 7
  • 36
  • 80
1

Use split with a regex:

String data="ggg;ggg;nnn;nnn;aaa;aaa;xxx;xxx;";
String [] array=data.split("(?<=\\G\\S\\S\\S;\\S\\S\\S);");
  • S: A non-whitespace character
  • G: last match/start of string, think of it of a way to skip delimiting if the previous string matches current one.
  • ?<=:positive look-behind will match semicolon which has string behind it.
Robert
  • 7,394
  • 40
  • 45
  • 64
HaroldSer
  • 2,025
  • 2
  • 12
  • 23
0

Some other answer, that only works given your specific example input.

You see, in your example, there are two similarities:

  1. All patterns seem to have exactly three characters
  2. All patterns occur exactly twice

In other words: if those two properties are really met for all your input, you could avoid splitting - as you know exactly what to find in each position of your string.

Of course, following the other answers for "real" splitting are more flexible; but (theoretically), you could just go forward and do a bunch of substring calls in order to directly access all elements.

GhostCat
  • 137,827
  • 25
  • 176
  • 248