猿问

两次检查一个字符串是否包含另一个字符串

我有一个遍历映射列表的 for 循环,现在我想检查映射列表的每个条目是否多次包含某个字符串,然后删除除第一个出现的字符串之外的所有字符串,但我不知道该怎么做.


for (Map<String, String> entry : mapList) {

    String line = "";

    for (String key : entry.keySet()) {

        if (StringUtils.containsAny(key, "5799"){

            line += entry.get(key) + "|";

        }

        list1.add(line);

    }

}

我感谢每一个想法。


侃侃无极
浏览 138回答 3
3回答

绝地无双

根据您的评论,我假设您的要求如下:您的字符串包含多个由竖线字符分隔的部分|,例如"a|e|b|c|a|c|a|d"你想删除所有重复的字符串,同时保留元素的顺序,例如你想要"a|e|b|c|d"为此,您可以在管道处拆分字符串,将元素收集到 a 中LinkedHashSet,然后使用管道重新加入元素。使用 Java 8 的示例://The pipe needs to be escaped because split() interprets the input as a regexSet<String> elements = new LinkedHashSet<>( Arrays.asList( input.split( "\\|" ) ) );//rejoin using the pipeString output = elements.stream().collect( Collectors.joining( "|" ) );

沧海一幻觉

要查看是否至少key包含一个字符串两次,并删除第二次出现,请使用两次,第二次调用在第一次出现后开始搜索:sindexOfstatic String removeSecond(String key, String s) {&nbsp; &nbsp; int idxFirst = key.indexOf(s);&nbsp; &nbsp; if (idxFirst != -1) {&nbsp; &nbsp; &nbsp; &nbsp; int idxSecond = key.indexOf(s, idxFirst + s.length());&nbsp; &nbsp; &nbsp; &nbsp; if (idxSecond != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return key.substring(0, idxSecond) +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;key.substring(idxSecond + s.length());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return key; // Nothing to remove}测试System.out.println(removeSecond("mississippi", "ss")); // prints: missiippiSystem.out.println(removeSecond("mississippi", "i"));&nbsp; // prints: missssippiSystem.out.println(removeSecond("mississippi", "pp")); // prints: mississippi更新如果您想删除所有重复项,即只保留第一次出现的项,请继续搜索。为了获得构建新字符串的最佳性能,请使用StringBuilder.static String removeDuplicates(String key, String s) {&nbsp; &nbsp; int idx = key.indexOf(s);&nbsp; &nbsp; if (idx == -1)&nbsp; &nbsp; &nbsp; &nbsp; return key; // Nothing to remove&nbsp; &nbsp; StringBuilder buf = new StringBuilder();&nbsp; &nbsp; int prev = 0;&nbsp; &nbsp; for (int start = idx + s.length(); (idx = key.indexOf(s, start)) != -1; prev = start = idx + s.length())&nbsp; &nbsp; &nbsp; &nbsp; buf.append(key.substring(prev, idx));&nbsp; &nbsp; return (prev == 0 ? key : buf.append(key.substring(prev)).toString());}测试System.out.println(removeDuplicates("mississippi", "ss")); // prints: missiippiSystem.out.println(removeDuplicates("mississippi", "i"));&nbsp; // prints: missssppSystem.out.println(removeDuplicates("mississippi", "s"));&nbsp; // prints: misiippiSystem.out.println(removeDuplicates("mississippi", "ab")); // prints: mississippi

慕标5832272

如果要删除除第一个以外的所有事件:public static String removeExceptFirst(String master, String child) throws Exception {&nbsp; &nbsp; int firstIndex = master.indexOf(child);&nbsp; &nbsp; int lastIndexOf = master.lastIndexOf(child);&nbsp; &nbsp; if (firstIndex == lastIndexOf) {&nbsp; &nbsp; &nbsp; &nbsp; if (firstIndex == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception("No occurrence!");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception("Only one occurrence!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; firstIndex = master.indexOf(child);&nbsp; &nbsp; &nbsp; &nbsp; lastIndexOf = master.lastIndexOf(child);&nbsp; &nbsp; &nbsp; &nbsp; if (firstIndex == lastIndexOf) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return master;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; master = master.substring(0, lastIndexOf) + master.substring(child.length() + lastIndexOf);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答