【问题标题】:What's the best way to look for a text fragment (selection in text) on an arraylist of fragments that compose that text?在组成该文本的片段数组列表上查找文本片段(文本中的选择)的最佳方法是什么?
【发布时间】:2020-02-17 09:50:48
【问题描述】:

我在查找 arraylist 上的文本巧合时遇到了麻烦。我有这个结构:

public class Fragment {
    private int id;
    private Date date;
    private String text;
    private Profile profile;
}

我有一个从 API 获取片段列表并将它们放在ArrayList<Fragment> 上的活动。它还创建了一个包含所有片段的 TextView。然后我有一个自定义的ActionMode.Callback 用于显示上下文菜单选项,例如“搜索”。

单击“搜索”菜单选项时,我想在该片段列表中搜索与所选文本的重合,但该文本不能是完全重合的片段。例如,将这个片段放在列表中:

fragments.get(0).getText() //returns "Hi"
fragments.get(1).getText() //returns ", Mi name is Peter"
fragments.get(2).getText() //returns ", how are you?"

而选中的文字是“is peter, how are”

感谢您的建议!

【问题讨论】:

    标签: java android string search arraylist


    【解决方案1】:

    下面是一个示例,说明您可以如何做到这一点。它基本上创建了一个基于全文匹配发生的边界,并找到与该边界重叠的片段。我添加了一些 cmets 以进行澄清。

    //Set up an example arraylist
    ArrayList<Fragment> fragments = new ArrayList<>();
    fragments.add(new Fragment("Hi"));
    fragments.add(new Fragment(", My name is Peter"));
    fragments.add(new Fragment(", how are you?"));
    
    //Create a full string of all fragments together
    String total = fragments.stream().map(Fragment::getText).collect(Collectors.joining());
    String toMatch = "Peter, how";
    
    //Search and check for a match in the full string, store the index where the match starts
    int matchIndex = total.indexOf(toMatch);
    if (matchIndex >= 0) {
        //Store the index where the match ends
        int maxMatchIndex = matchIndex + toMatch.length();
        ArrayList<Fragment> overlap = new ArrayList<>();
        //Keep track of the current character index we're at compared to the full string
        int processed = 0;
        for (Fragment fragment : fragments) {
            //Add any fragments that lie between the start and end index of the match to the overlap arraylist
            if(processed + fragment.getText().length() >= matchIndex && processed < maxMatchIndex) {
                overlap.add(fragment);
            }
            //Add the length of the processed fragment to update the "full string index" for the next check
            processed += fragment.getText().length();
        }
    
        //Do something with the match, here we just print the text
        for (Fragment l : overlap) {
            System.out.println("Fragment match: " + l.getText());
            //Prints:
            //  Fragment match: , My name is Peter
            //  Fragment match: , how are you?
        }
    }
    

    请注意,我创建了一个用于插入文本的基本构造函数和一个用于比较文本的 getter。

    【讨论】:

    • 如果要搜索的选定字符串是:“Peter, how are”呢?
    • @IanOrtega 在这种情况下,您可能必须将所有片段拼接在一起,检查匹配发生的索引并从那里解析它重叠的所有片段。如果你愿意,我可以写一个例子
    • 我正在考虑创建一个HashMap&lt;String, Fragment&gt;,其中字符串的组成类似于startIndex+"-"+endIndex,文本上的片段开始索引,文本上的片段结束索引和片段本身,然后得到所选文本的开始和结束索引以及在地图上搜索...
    • @IanOrtega 我已将我的答案更新为一个应该反映您所问内容的示例。它的功能与您的想法非常相似:)
    • 没问题,这是一个有趣的问题,很快就能看出来。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2019-02-27
    相关资源
    最近更新 更多