简体   繁体   English

在Android Studio中获取数组中两个字符串之间的所有字符串

[英]Getting all the strings between two strings in an array in Android Studio

I'm looking for a way to grab all the strings between two strings in an array.我正在寻找一种方法来获取数组中两个字符串之间的所有字符串。

String[] list = inputOCR.split("(?=\\p{Space})"); String[] list = inputOCR.split("(?=\\p{Space})"); = this is the way I built the array. = 这是我构建阵列的方式。

The order of the strings goes like [Other,Name, Middle, First, Last,Address, word, word, word, word, word, word, License, etc, etc, etc, etc] How do I get all the words between address and license inside the array?字符串的顺序是 [其他,姓名,中间,名字,姓氏,地址,单词,单词,单词,单词,单词,单词,许可证等,等等,等等] 如何获取所有单词之间的阵列内的地址和许可证?

Update: The index of the array isnt fixed based on the OCR scan,so I also have to look for the word "Address", and "License" in the array.更新:基于 OCR 扫描,数组的索引不是固定的,因此我还必须在数组中查找单词“Address”和“License”。

Ah yes, indexes can be confusing!是的,索引可能会令人困惑! I've got some code that might help you along.我有一些代码可以帮助你。 What you want to do, once you've split the words into an array, is find the index (position) of the words that are the start and end of your search.将单词拆分为数组后,您想要做的是找到作为搜索开始和结束的单词的索引(位置)。 In this case, "address" and "license".在这种情况下,“地址”和“许可证”。 Then you can loop between these values to get the words you need.然后您可以在这些值之间循环以获取您需要的单词。

public class GetWords {

    public static void main(String[] args) {
        String inputOCR = "This contains some address random words used to test License should not be picked up";
        String[] list = inputOCR.split(" "); 

        int startPosition = getIndexOfWordInArray(list, "address") + 1;
        int endPosition = getIndexOfWordInArray(list, "license");

        for (int i = startPosition; i < endPosition; i++) {
            System.out.println(list[i]);
        }
    }

    public static int getIndexOfWordInArray(String[] list, String word) {
        int index = -1;

        for (int i = 0; i < list.length; i++) {
            if (list[i].equalsIgnoreCase(word)) {
                index = i;
                break;
            }
        }

        return index;
    }
}

The code above splits the string on spaces, then gets the positions of "address" and "license" from the index.上面的代码在空格上拆分字符串,然后从索引中获取“地址”和“许可证”的位置。 It then loops through the array, printing the words between those two positions.然后循环遍历数组,打印这两个位置之间的单词。 The code adds 1 to the position of "address", because you want to exclude it and it stops just before the position of "license".代码在“address”的位置加1,因为你想排除它,它就停在“license”的位置之前。

Instead of printing the words, you can add them to an ArrayList, which is an array that can grow dynamically, depending on how much you put into it.您可以将它们添加到 ArrayList 中,而不是打印单词,这是一个可以动态增长的数组,具体取决于您放入其中的数量。

I tried to keep the code as simple and beginner-friendly as possible, but please ask if it's still unclear.我尽量保持代码简单和初学者友好,但请询问是否仍然不清楚。

Ok if I got you right, I hope I did, this should do the trick:好吧,如果我猜对了,我希望我做到了,这应该可以解决问题:

// Code begins with a splitted array so String[] list exists already
int address = 0; // this will determine where your Address is located in the array
int license = -1; // this will determine where license is located in the array

for (int i = 0; i < list.length; i++) {
    if (list[i].equals("Address"))
        address = i;
    else if (address != 0 && list[i].equals("License")) {
        license = i;
        break;    // loop can be left as everything else is unimportant
    }
}

String[] result = new String[license-address-1]; // This will throw an error on purpose if license and/or address were not in list
for (int i = address+1; i < license; i++)
    result[i-address-1] = list[i]; // actual copying
// Be happy with your result String[]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM