如何从 sparseArray 中减去单个字符串?

How to substract single string from a sparseArray?

我使用 Android Mobile Vision OCR API 已经有一段时间了。一切都很完美,直到我发现我只需要从整个 SparseArray 中提取单个单词(移动视觉 API 默认 return 是在 SparseArray 中定义的 TextBlocks)

SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);

  for (int i = 0; i < textBlocks.size(); i++) {

       TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
            List<Line> lines = (List<Line>) textBlock.getComponents();
            for (Line line : lines) {
                List<Element> elements = (List<Element>) 
                line.getComponents();
                for (Element element : elements) {
                    word = element.getValue();

                    Log.d(TAG, "word Read : " + word);
                }
            }
        }

当我检查时

Log.d(TAG, "word Read : " + word);

它重复打印出 SparseArray 中的所有元素

看来我在问一个不太明显的问题。但是我可以从上面打印的 "words" 中提取一个或几个单词吗?例如,我想提取字符大于 12 且其中包含数字的单词。

任何帮助或提示将不胜感激。

您可以添加逻辑表达式来过滤结果,如下所示:

    word = element.getValue();
    if (word .length() > 12 && word .matches("[0-9]+")) {
        Log.d(TAG, "word Read : " + word);
    }

您正在循环 运行ning 单词,这就是它打印所有值的原因。根据@navylover 的回答,当你 运行 它只有一次时,你将得到一个字符串。只需删除 for 循环