尝试字符串中的所有空格组合

时间:2014-11-05 08:18:17

标签: java string

我想使用添加了空格的给定字符串的所有可能组合来制作 for循环

示例:

t hefatcow
th efatcow
th efatcow
the fatcow
thef atcow
etc...

最简单的方法是什么?

编辑:我实际上是想用" _"下划线,而不是空格。

1 个答案:

答案 0 :(得分:4)

如果我理解你的问题,那么你可以用

之类的东西来做
String word = "thefatcow";
for (int i = 1; i < word.length(); i++) {
    System.out.printf("%s %s%n", word.substring(0, i),
            word.substring(i));
}

输出

t hefatcow
th efatcow
the fatcow
thef atcow
thefa tcow
thefat cow
thefatc ow
thefatco w
相关问题