How to split a string by every other separator

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;" ???????

Upvotes: 5

Views: 1935

Answers (5)

HaroldSer
HaroldSer

Reputation: 2065

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.

Upvotes: 1

cody123
cody123

Reputation: 2080

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

Upvotes: 1

Erik
Erik

Reputation: 221

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;

Upvotes: 3

nick zoum
nick zoum

Reputation: 7325

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;
}

Upvotes: 1

GhostCat
GhostCat

Reputation: 140633

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.

Upvotes: 0

Related Questions