查找字符串中多个单词的索引

时间:2016-09-06 00:01:34

标签: java indexing

我发誓我这样做了一会儿就行了。但我不记得我是怎么做到的。我想在字符串中找到多个单词的索引。以下代码仅适用于一个单词。我需要一个更有说服力的解决方案来处理几个问题。

import java.util.*;

public class test {

    public static List<List<Boolean>> work;

    public static void main (String[] args) {
        String test = "I'm trying to both extract and replaces. This that and the other";
        String word = "I'm";

        for (int i = -1; (i = test.indexOf(word, i + 1)) != -1; ) {


            System.out.println(i);
        }
    }
}

我想说我想找到“both”和“other”的索引。

4 个答案:

答案 0 :(得分:0)

哥们......我试图指出你正确的方向......不给你完整的工作代码:)这是一个完整的工作代码:

public static void main (String[] args) 
{
    String test = "I'm trying to both extract and replaces. This that and the other";
    String[] words = {"I'm", "other", "both"};

    for (int j=0; j < words.length; j++) 
    {           
        System.out.println("Word: " + words[j] + " is at index: " + test.indexOf(words[j]));
    }
}

这样,您就不会获得ArrayIndexOutOFBounds异常。你要经历一次每个单词。

但是对于每个单词,你都要经历一次完整的测试字符串。在您之前的代码中,您在循环中使用了i = test.indexOf(word)。如果单词不是出现在测试字符串中的顺序,则会产生问题。

例如:test =“我是摇滚明星”          words = {“rockstar”,“a”} 在这种情况下,循环变量i向前移动到找到'rockstar'的索引,但之后不会找到'a',因为测试字符串在'rockstar'之后不包含'a'。

但是在我发布的当前代码中,单词的顺序可以是任何内容。

希望清楚。

答案 1 :(得分:0)

我做了更多研究,这似乎是你要求的:

String test = "I'm trying to both extract and replaces. This that and the other";

    String[] words = test.split("\\s+");
    for (int i = 0; i < words.length; i++) {
        words[i] = words[i].replaceAll("[^\\w]", "");
    }

    System.out.println(Arrays.asList(words).indexOf("both"));

结果: 3

请记住,Java计数包括0,所以&#34;两者都是&#34;实际上是我们人类的指数4。

----旧解决方案----

关于他们想要什么的问题很模糊,但这将通过HashMap返回与每个单词对应的索引号:

HashMap<Integer, String> hmap = new HashMap<Integer, String>();
    String test = "I'm trying to both extract and replaces. This that and the other";

    String[] words = test.split("\\s+");
    for (int i = 0; i < words.length; i++) {
        words[i] = words[i].replaceAll("[^\\w]", "");
    }

    for(int i = 0; i < words.length; i++){
        hmap.put(i, words[i]);
    }

    System.out.println(hmap);

结果:

{0=Im, 1=trying, 2=to, 3=both, 4=extract, 5=and, 6=replaces, 7=This, 8=that, 9=and, 10=the, 11=other}

答案 2 :(得分:0)

我相信你引用了这个SO question的答案。要将它用于多个单词,您只需要一个简单的数据结构来存储您想要查找的单词,循环遍历该单词,并返回包含该单词及其相应位置的地图,如下所示:

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class WordFinder {

    public static void main(String[] args) {
        String phrase = "0123hello9012hello8901hello7890";
        String[] words = { "hello", "90" };

        Map<String, Stack<Integer>> dest = findWordsInString(words, phrase);

        System.out.println(dest.toString());
    }

    public static Map<String, Stack<Integer>> findWordsInString(String[] words, String phrase) {
        Map<String, Stack<Integer>> dest = new HashMap<>();

        for (String word : words) {
            Stack<Integer> locations = new Stack<>();

            for (int loc = -1; (loc = phrase.indexOf(word, loc + 1)) != -1;) {
                locations.add(loc);
            }

            dest.put(word, locations);
        }

        return dest;
    }
}

节目输出: {90=[9, 19, 29], hello=[4, 13, 22]}

答案 3 :(得分:0)

./test.sh -o c03 -d mydb -t tblA -n c13 -r us-east-1
./test.sh -o c03 -d mydb -t tblB -n c13 -r us-east-1
./test.sh -o c03 -d mydb -t tblC -n c13 -r us-east-1

输出中

public static void main (String[] args)
{
    String test="I'm trying to both extract and replaces. This that and the other";
    String[] words={"other","This","that"};
    for(int j=0;j<words.length;j++)
    {
        for(int i=-1;(i=test.indexOf(words[j],i+1))!=-1;)
        {
            System.out.println(words[j]+" "+ i);
        }
    }
}
相关问题