2014-11-05 31 views
-3

我想用作爲循環,它與給定字符串的所有可能組合一起添加了空格。試試字符串中空格的所有組合

例子:

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

什麼是最簡單的方法是什麼?

編輯:我實際上是想用「_」做下劃線,而不是空格。

+4

嘗試something..That對你最簡單的方法.. – CHOCKO 2014-11-05 08:19:35

+1

你嘗試過什麼至今? – Rohan 2014-11-05 08:21:22

+1

我不知道我所問的點。最後我檢查了堆棧溢出不是僅用於代碼校正。你們有很多場景的經驗,我希望這是其中之一。 – ThatGuy343 2014-11-05 08:22:03

回答

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 
+0

完美運作。當SO讓我時,會標記爲答案 – ThatGuy343 2014-11-05 08:27:48

相關問題