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

Java 尝试字符串中的所有空格组合,java,string,Java,String,我想用给定字符串的所有可能组合创建一个for循环,并添加空格 例如: t hefatcow th efatcow th efatcow the fatcow thef atcow etc... 最简单的方法是什么 编辑:我实际上是想用下划线而不是空格来做这件事。如果我理解你的问题,那么你可以用类似 String word = "thefatcow"; for (int i = 1; i < word.length(); i++) { System.out.printf("%s %

我想用给定字符串的所有可能组合创建一个for循环,并添加空格

例如:

t hefatcow
th efatcow
th efatcow
the fatcow
thef atcow
etc...
最简单的方法是什么


编辑:我实际上是想用下划线而不是空格来做这件事。

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

String word = "thefatcow";
for (int i = 1; i < word.length(); i++) {
    System.out.printf("%s %s%n", word.substring(0, i),
            word.substring(i));
}

试试看这是你最简单的方法到目前为止你试过什么吗?我还没试过这是我想问的问题。上次我检查堆栈溢出不仅仅是为了代码更正。你们有很多场景的经验,我希望这是其中之一。然而,这也不是一个为我做事的网站。你应该尝试一下,在你遇到问题的特定地方寻求帮助。这不是关于我是否感谢@reto的帮助,我感谢海报并感谢他的回答。但我有没有在任何地方要求代码?你们也可以很容易地发布伪代码。工作非常完美。这样的话我会记下答案的
t hefatcow
th efatcow
the fatcow
thef atcow
thefa tcow
thefat cow
thefatc ow
thefatco w