Home > Mobile >  Java Separate a String using a List of Words
Java Separate a String using a List of Words

Time:01-09

How could I separate a String using a pre-given List of Strings, separating them by spaces?

Eg:

List of words: words = {'hello', 'how', 'are', 'you'}

String I want to separate: 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;
}

And new_text should return hello how are you

Note that the order of the List words could be different and also have more words, like a dictionary.

How could I make this recursion, if needed?

CodePudding user response:

This should do what you want

  • You should use StringBuilder if you find yourself repeatedly appending to a string
  • Use a while loop to iterate through text, remove one word at a time and finish when text is empty
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();
    }
}

CodePudding user response:

How could I separate a String using a pre-given List of Strings, separating them by spaces?

Pretty much how you already started. Checking if the remaining text starts with any of the words from the list, remove the starting word and keep the suffix.

You did all that already, but instead of just keeping the suffix and keep iterating you decided to try to call separateText recursively.

That is also a possibility, but even just normally iterating in a while loop until the suffix (or remaining text) is empty is enough.

Using a loop like while (index < text.length()) will work for longer inputs too even if the words are in a different order.

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();
}
  •  Tags:  
  • Related