简体   繁体   English

如何从Java中的特定索引获取关键字

[英]How to get keywords from a specific index in Java

Suppose I have a string: 假设我有一个字符串:

String advise = "eat healthy food";

In the string I only know the keyword “healthy”. 在字符串中我只知道关键字“健康”。 I don't know what has before the word nor what has after the word. 我不知道这个词之前有什么,也不知道之后的是什么。 I just only know the middle word. 我只知道中间词。 So how can I get the before (“eat”) and after (“food”) keyword of “healthy”? 那么我怎样才能获得“健康”的之前(“吃”)和之后(“食物”)关键词?

Note: Here the middle word's size is always specfic but the other two word's size is always different. 注意:这里中间词的大小总是特定的,但另外两个词的大小总是不同的。 Here “eat” and “food” have been used as an example only. 这里仅以“吃”和“食物”为例。 These two words may be anything anytime. 这两个词可能随时都有。

I need to get these two words into two different strings, not in the same string. 我需要将这两个单词分成两个不同的字符串,而不是在同一个字符串中。

Just split the string: 只需拆分字符串:

advise.split("healthy");

The first value in the array will be "eat", the second will be "food". 数组中的第一个值是“吃”,第二个值是“食物”。

Here is a more general purpose solution that will handle more complex strings. 这是一个更通用的解决方案,可以处理更复杂的字符串。

public static void main (String[] args)
{
    String keyword = "healthy";

    String advise = "I want to eat healthy food today";

    Pattern p = Pattern.compile("([\\s]?+[\\w]+[\\s]+)" + keyword + "([\\s]+[\\w]+[\\s]?+)");
    Matcher m = p.matcher(advise);

    if (m.find())
    {
        String before = m.group(1).trim();
        String after = m.group(2).trim();

        System.out.println(before);
        System.out.println(after);
    }
    else
    {
        System.out.println("The keyword was not found.");
    }
}

Outputs: 输出:

eat

food 餐饮

I think you can use split and get all the words separately as you wanted. 我认为您可以使用拆分并根据需要单独获取所有单词。

String advise = "eat healthy food";
String[] words = advise.split("healthy");
List<String> word = Arrays.asList(words);
word.forEach(w-> System.out.println(w.trim())); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从java中的字符串中获取特定索引中的字符 - How to get characters from a specific index from a string in java 如何从JSON文件Java中的特定索引获取数据 - How to get the data from specific index in json file java Java-Files:从文本文件中获取特定索引 - Java-Files: get specific index from text file Java 8:如何从Stream的索引处获取元素 - Java 8: How to get element at an index from a Stream 如何从Java中的索引获取枚举值? - How to get Enum Value from index in Java? 如何从 java 中的列表中删除子列表,其中特定 substring 作为开始索引,特定 substring 作为结束索引? - How to delete sublists from a list in java with specific substring as a start index and specific substring as an end index? 如果存在特定的DynamoDB索引,如何从Java API进行检查 - How to check from Java API if specific DynamoDB index exists 如何在Jersey + Java中返回特定索引的数组列表 - How do you return in Jersey + Java an arraylist from a specific index 在 Java 中,如何从数组中的特定索引向下计数? - How do I count from a specific index in an array on downwards in Java? 如何从Java中的arraylist返回特定的对象(索引)? - How can I return a specific object (index) from an arraylist in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM