【问题标题】:Getting all the strings between two strings in an array in Android Studio在Android Studio中获取数组中两个字符串之间的所有字符串
【发布时间】:2020-02-29 08:37:53
【问题描述】:

我正在寻找一种方法来抓取数组中两个字符串之间的所有字符串。

String[] list = inputOCR.split("(?=\p{Space})"); = 这是我构建数组的方式。

字符串的顺序类似于 [Other,Name, Middle, First, Last,Address, word, word, word, word, word, word, License, etc, etc, etc, etc] 如何获取数组中地址和许可证之间的所有单词?

更新:根据OCR扫描,数组的索引不是固定的,所以我还要在数组中寻找“地址”和“许可证”这个词。

【问题讨论】:

  • 你有什么尝试吗?基本解决方案是遍历数组并提取两个单词之间的所有内容
  • 我正在尽最大努力学习,但由于我不完全理解循环遍历数组的逻辑,所以我现在卡住了。

标签: java android arrays


【解决方案1】:

啊,是的,索引可能会令人困惑!我有一些代码可以帮助你。将单词拆分为数组后,您要做的是找到作为搜索开始和结束的单词的索引(位置)。在这种情况下,“地址”和“许可证”。然后你可以在这些值之间循环来获取你需要的单词。

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;
    }
}

上面的代码用空格分割字符串,然后从索引中获取“地址”和“许可证”的位置。然后它遍历数组,打印这两个位置之间的单词。代码在“address”的位置加1,因为你想排除它,它就停在“license”的位置之前。

您可以将它们添加到 ArrayList 中,而不是打印单词,这是一个可以动态增长的数组,具体取决于您放入其中的数量。

我试图让代码尽可能简单且对初学者友好,但如果还不清楚,请询问。

【讨论】:

  • 考虑到他是初学者,我真的很喜欢你的回答!
  • 哇,真的很感激!你真的让初学者尽可能简单地理解代码的逻辑。这么大的帮助!!谢谢!
  • 我只是有一个问题。在将提取的单词放入 textview 时, textView.append(list[i]) 是否正确?或者我必须在 setText 到 textview 之前将它分配给一个字符串?
  • @wazzar 当然,您可以随意显示它。我会把它放在 ViewModel 中,这样它就可以在设备旋转中幸存下来。追加时,也可以使用 append("\n") 添加换行符。玩得开心,Android 开发是一场冒险。
  • 似乎无法正常工作。但仍在寻找问题。将尝试登录
【解决方案2】:

好吧,如果我没听错的话,我希望我做到了,这应该可以解决问题:

// 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[]

【讨论】:

  • 啊,专业的答案!很好 - 我真的很喜欢这里的优化。
  • 真的很有帮助!尝试使用您的代码,但我遇到了问题。它抓取从地址之后开始的字符串,但它不会在“许可证”之前停止。我该如何解决这个问题?
  • 对不起!我的错。忘记在循环中插入中断。现在完美运行。感谢您和@Ewald 帮助初学者!真的很感激!
  • 嗨@wazzar,由于时区,我错过了这个。非常棒的推动力,这就是发展的意义所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 2012-12-28
  • 1970-01-01
相关资源
最近更新 更多