从字符串中查找单词的下一个单词

时间:2019-04-26 08:48:31

标签: java string

我编写了以下代码来从Java字符串中获取下一个单词。我觉得它很原始,我不必为此写太多代码,但找不到其他方法。想知道是否有更好的方法可以做到这一点:

public static String getNextWord(String str, String word) {
    String nextWord = null;
    // to remove multi spaces with single space
    str = str.trim().replaceAll(" +", " ");
    int totalLength = str.length();
    int wordStartIndex = str.indexOf(word);
    if (wordStartIndex != -1) {
        int startPos = wordStartIndex + word.length() + 1;
        if (startPos < totalLength) {
            int nextSpaceIndex = str.substring(startPos).indexOf(" ");
            int endPos = 0;
            if (nextSpaceIndex == -1) {
                // we've reached end of string, no more space left
                endPos = totalLength;
            } else {
                endPos = startPos + nextSpaceIndex;
            }
            nextWord = str.substring(startPos, endPos);
        }
    }
    return nextWord;
}

注意:输入的单词可以是任何单词(多个单词,单个单词,非字符串形式的单词等)。

测试:

    String text = "I am very happy with life";

    System.out.println(StringUtil.getNextWord(text, "I"));
    System.out.println(StringUtil.getNextWord(text, "I am"));
    System.out.println(StringUtil.getNextWord(text, "life"));
    System.out.println(StringUtil.getNextWord(text, "with"));
    System.out.println(StringUtil.getNextWord(text, "fdasfasf"));
    System.out.println(StringUtil.getNextWord(text, text));

输出:

am
very
null
life
null
null

6 个答案:

答案 0 :(得分:2)

这听起来像是正则表达式的工作。像这样:

public static String getNextWord(String str, String word){
    Pattern p = Pattern.compile(word+"\\W+(\\w+)");
    Matcher m = p.matcher(str);       
    return  m.find()? m.group(1):null;      
} 

答案 1 :(得分:1)

我认为,此解决方案可以正常工作:

public static String getNextWord(String str, String word) {
    String[] strArr = str.split(word);
    if(strArr.length > 1) {
        strArr = strArr[1].trim().split(" ");
        return strArr[0];
    }
    return null;
}

答案 2 :(得分:0)

您可以执行以下操作来创建单词数组:

String[] words = str.split(" ");

当用空格分隔时,这会将字符串拆分为多个字符串。请注意,您仍然需要根据需要修剪str。 现在,您可以通过查找某个单词并将索引加1来获得下一个单词,从而以某种方式在数组中进行搜索。

nextword = words[words.indexOf(word) + 1];

答案 3 :(得分:0)

希望这就是您要寻找的东西:

public static void main(String[] args) {
    String text = "I am very happy with life";
    System.out.println(getNextWord(text,"am"));
    System.out.println(getNextWord(text,"with"));
    System.out.println(getNextWord(text,"happy"));
    System.out.println(getNextWord(text,"I"));
    System.out.println(getNextWord(text,"life"));
}
public static String getNextWord(String text,String finditsNext){
    String result = "There is no next string";
    try {
        int findIndex = text.indexOf(finditsNext);
        String tep = text.substring(findIndex);
        if(tep.indexOf(" ") >0) {
        tep = tep.substring(tep.indexOf(" ") + 1);
        if(tep.indexOf(" ") >0)
            result = tep.substring(0, tep.indexOf(" "));
        else
            result = tep;
        }
    }catch (IndexOutOfBoundsException ex){

    }
    return result;
}

上面的输出是:

very
life
with
am
There is no next string

答案 4 :(得分:0)

希望这会达到您的目的。

public static String getNextWord(String str, String word) {
    String[] words = str.split(" "), data = word.split(" ");
    int index = Arrays.asList(words).indexOf((data.length > 1) ? data[data.length - 1] : data[0]);
    return (index == -1) ? "Not Found" : ((index + 1) == words.length) ? "End" : words[index + 1];
}

输入(单个单词):

String str = "Auto generated method stub";
String word = "method";

输出:

next word: stub

输入(多字):

String str = "Auto generated method stub";
String word = "Auto generated";

输出:

next word: method 

输入(缺少单词):

String str = "Auto generated method stub";
String word = "was";

输出:

next word: Not Found 

输入(结束词):

String str = "Auto generated method stub";
String word = "stub";

输出:

next word: End 

答案 5 :(得分:0)

您可以尝试以下代码。

public static String getNextWord(String str, String word) {
    try {
        List<String> text = Arrays.asList(str.split(" "));
        List<String> list = Arrays.asList(word.split(" "));
        int index_of = text.indexOf(list.get(list.size() - 1));
        return (index_of == -1) ? null : text.get(index_of + 1);
    } catch(Exception e) {
        return null;
    }
}
相关问题