Java,如何使用shift来拆分String

rei*_*eki 9 java regex string split

如何用2字符拆分字符串shifting.例如;

我的字符串是= todayiscold

我的目标是: "to","od","da","ay","yi","is","sc","co","ol","ld"

但是这个代码:

Arrays.toString("todayiscold".split("(?<=\\G.{2})")));
Run Code Online (Sandbox Code Playgroud)

我得到:`"to","da","yi","co","ld"

有人帮忙吗?

nku*_*har 7

试试这个:

        String e = "example";
        for (int i = 0; i < e.length() - 1; i++) {
            System.out.println(e.substring(i, i+2));
        }
Run Code Online (Sandbox Code Playgroud)

  • @Jason我同意但高效的解决方案通常比较复杂,在这里我提出简单易懂的解决方案.这个问题根本不是关于绩效的. (2认同)