如何使用預先給定的字串串列來分隔字串,并用空格分隔它們?
例如:
單詞串列: words = {"hello", "how", "are", "you"}
我要分離的字串: text = "hellohowareyou"
public static String separateText(String text, List<String> words) {
String new_text;
for (String word : words) {
if (text.startsWith(word)) {
String suffix = text.substring(word.length()); //'suffix' is the 'text' without it's first word
new_text = " " word; //add the first word of the 'string'
separateString(suffix, words);
}
}
return new_text;
}
并且new_text應該回傳hello how are you
請注意,串列的順序words可能不同,并且可能包含更多單詞,例如字典。
如果需要,我怎樣才能進行這種遞回?
uj5u.com熱心網友回復:
這應該做你想做的
- 如果您發現自己反復附加到字串,則應該使用 StringBuilder
- 使用 while 回圈遍歷
text,一次洗掉一個單詞并在text為空時完成
public static String separateText(String text, List<String> words){
StringBuilder newTextBuilder = new StringBuilder();
outerLoop:
while(text.length() > 0){
for(String word : words){
if(text.startsWith(word)){
newTextBuilder.append(word " ");
text = text.substring(word.length());
continue outerLoop;
}
}
}
return newTextBuilder.toString();
}
}
uj5u.com熱心網友回復:
如何使用預先給定的字串串列來分隔字串,并用空格分隔它們?
幾乎你已經開始了。檢查剩余文本是否以串列中的任何單詞開頭,洗掉起始單詞并保留后綴。
您已經完成了所有這些,但是您決定嘗試separateText遞回呼叫,而不是僅僅保留后綴并繼續迭代。
這也是一種可能,但即使只是正常地在 while 回圈中迭代直到后綴(或剩余的文本)為空就足夠了。
while (index < text.length())即使單詞的順序不同,使用類似回圈也適用于更長的輸入。
public String separateText(String text, List<String> words){
if (text == null) return "";
if (words == null || words.isEmpty()) return text;
StringBuilder sb = new StringBuilder();
boolean unknownWord = false;
int index = 0;
while (index < text.length()) {
boolean wordFound = false;
for (String word : words) {
if (!word.isEmpty() && text.startsWith(word, index)) {
wordFound = true;
// move the index ahead just past the last letter of the word found
index = word.length();
if (unknownWord) {
unknownWord = false;
sb.append(" ");
}
sb.append(word);
sb.append(" ");
break;
}
}
if (!wordFound) {
unknownWord = true;
sb.append(text.charAt(index));
index ;
}
}
return sb.toString();
}
uj5u.com熱心網友回復:
這個解決方案非常簡單,但它不是記憶體最佳的,因為String創建了許多新的。
public static String separate(String str, Set<String> words) {
for (String word : words)
str = str.replace(word, word ' ');
return str.trim();
}
演示
Set<String> words = Set.of("hello", "how", "are", "you");
System.out.println(separate("wow hellohowareyouhellohowareyou", words));
// wow hello how are you hello how are you
另一種解決方案,StringBuilder從性能角度來看,我看起來更好。
public static String separate(String str, Set<String> words) {
List<String> res = new LinkedList<>();
StringBuilder buf = new StringBuilder();
for (int i = 0; i < str.length(); i ) {
buf.append(str.charAt(i));
if (str.charAt(i) == ' ' || words.contains(buf.toString())) {
res.add(buf.toString().trim());
buf.delete(0, buf.length());
}
}
return String.join(" ", res);
}
uj5u.com熱心網友回復:
對于遞回方法,請嘗試以下操作:
public static String separateText(String text, List<String> words){
return separateText(text, words, new StringBuilder());
}
public static String separateText(String text, List<String> words, StringBuilder result){
for(String word : words){
if (text.startsWith(word)){
result.append(word).append(" ");
text = text.substring(word.length());
ArrayList<String> newList = new ArrayList<>(words);
newList.remove(word);
separateText(text, newList, result);
break;
}
}
return result.toString().trim();
}
uj5u.com熱心網友回復:
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// You must sort this by it's length, or you will not have correct result
// since it may cause match with more shorter words.
// In this example, it's done
List<String> words = Arrays.asList("hello", "how", "are", "you");
List<String> detectedWords = new ArrayList<>();
String text = "hellohowareyou";
int i = 0;
while (i < text.length()) {
Optional<String> wordOpt = Optional.empty();
for (String word : words) {
if (text.indexOf(word, i) >= 0) {
wordOpt = Optional.of(word);
break;
}
}
if (wordOpt.isPresent()) {
String wordFound = wordOpt.get();
i = wordFound.length();
detectedWords.add(wordFound);
}
}
String result = String.join(" ", detectedWords);
System.out.println(result);
}
}
我以為:
- 你的文字永遠不會
null - 您的文字與正則運算式匹配
^(hello|how|are|you)$ - 你的話必須排序
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/417802.html
標籤:
上一篇:遞回函式多次列印零
