【问题标题】:Replace String and check for uniqueness within String替换字符串并检查字符串中的唯一性
【发布时间】:2015-09-09 20:50:20
【问题描述】:

我正在尝试用字符串中的空值替换特定字符串。但在 sn-p 下面没有删除空格。还有没有简单的方法来寻找用字符串分隔我的分隔符的唯一性?

 String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR||";
 String newStr = str.replaceAll("RAI", "");
 System.out.println("Updates String is::"+newStr);

我正在寻找的输出是 ||MGR||PRE MGR||PRE SPR||

谢谢

【问题讨论】:

  • 既然字符串在Java中是不可变的,你可能想考虑将要过滤的元素存储在某个Collection中并修改你的Collection的内容,而不是使用这种方法修改String?跨度>
  • "RAI" 在我看来不像一个正则表达式。如果您不知道那是什么,那么您应该使用replace() 而不是replaceAll()。它仍然会替换所有出现的事件。

标签: java string replace


【解决方案1】:

感谢您的所有意见。根据 cmets 和建议,我设法使用以下代码获得所需的输出

    String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR||";
    String noDupBRole = "||";

    String newStr = str.replaceAll("RAI ", "").replaceAll(" RAI", "").replaceAll("RAI", "");
    System.out.println("New String is::"+newStr);
    Set<String> set = new LinkedHashSet<String>(Arrays.asList(newStr.split(Pattern.quote("||"))));

    for(String st : set) {
        if(st.isEmpty()) continue;
        noDupBRole += st+"||";
    }

    System.out.println("No Duplicate ::"+noDupBRole);

【讨论】:

    【解决方案2】:

    Replace 省略“RAI”或“RAI”的字符串,split 检查值的唯一性,create 具有唯一值的新字符串。

    String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR";
    String newStr = "||";
    str = str.replaceAll("( |)RAI", "");
    String[] values = str.split("\\|\\|");
    
    //add only unique values to new string
    for (int i = 1; i < values.length; i++) {
      if(!newStr.contains(values[i].trim())){
        newStr += values[i].trim() + "||"; 
      }
    }
    System.out.println("Updates String is::" + newStr);
    

    【讨论】:

      【解决方案3】:

      replaceAll String 方法采用正则表达式,因此您可以传递一个并使用它来代替

      String newStr = str.replaceAll("\\s?RAI\\s?", "");

      [编辑] 您只需要删除尾随空格即可获得所需的输出。使用

      String newStr = str.replaceAll("RAI\\s?", "");

      完整代码:

      String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR||";
      String newStr = str.replaceAll("RAI\\s?", "");
      Set<String> parts = new LinkedHashSet<String>(Arrays.asList(newStr.split("||")));
      StringBuilder sdb = new StringBuilder("||");
      for (String part : parts) {
          sdb.append(part).append("||");
      }
      newStr = sdb.toString();
      System.out.println("Updates String is::" + newStr);
      

      【讨论】:

      • 这可能会被赞成,但它并不能确保 || 分隔符之间每次运行的唯一性。
      • 这个正则表达式正在删除 RAI。但是一旦形成新的字符串,我怎样才能删除重复项。我可以使用模式而不是集合吗?
      • 您不能将它们全部替换为正则表达式。这需要比离散有限自动机更多的计算能力。您无法使用字符串替换(轻松地)解决重复数据删除问题,因为您不必“replaceAll(...)”(关于重复数据删除)而是“replaceAllAfterTheFirst(...)”。简而言之,实际拆分字符串然后重建它要好得多,使用 Set 来“保留一个仅包含唯一值的列表”。
      • @EdwinBuck 我同意你的看法。添加了执行重复数据删除和保留顺序的代码。
      【解决方案4】:

      在 replaceAll() 正则表达式中包含一个空格,然后使用 Java 8,您可以删除重复项。否则,您必须自己手动删除重复项,这仍然是可能的,但为什么要重新发明轮子(学习目的除外)。

      public static void main(String[] args) throws Exception {
          String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR||";
          // "RAI\\s?" means there may be a single space after "RAI"
          String newStr = str.replaceAll("RAI\\s?", "");
          System.out.println("Updates String is:: " + newStr);
          // Remove duplicates
          System.out.println("Duplicates Removed:: " + Arrays.stream(
                  newStr.split("(?=\\|\\|)"))
                      .distinct()
                      .map(s -> (s))
                      .collect(Collectors.joining()));
      }
      

      结果:

      Updates String is:: ||MGR||MGR||PRE MGR||PRE SPR||PRE SPR||
      Duplicates Removed:: ||MGR||PRE MGR||PRE SPR||
      

      【讨论】:

        【解决方案5】:

        --- 编辑更新---

        等一下,你正在这里做很多事情。您不仅在进行字符串替换,还压缩了 || 分隔符之间的字段,这样您就没有具有相同内容的重复字段。

        如果您只是剥离“RAI”,那么您将拥有

        ||MGR||MGR||PRE MGR||PRE SPR||PRE SPR||
        

        所以,首先,沿着|| 分隔符将所有字段拆分为字符串。然后剥离不需要的“RAI”的每个字符串。将它们添加到Set&lt;String&gt;,然后从Set 中的项目重建输入字符串。

        --- 原帖如下---

        使用您正在驾驶的技术,您将获得一个包含两个空格的部分,这是因为“PRE RAI MGR”将压缩为“PRE MGR”。

        一个技巧是将“RAI”替换为“”,然后将“RAI”替换为“”,最后将“RAI”替换为“”

        【讨论】:

          【解决方案6】:

          试试:

          String newStr = str.replaceAll("RAI ", "").replaceAll(" RAI", "").replaceAll("RAI", "");
          

          【讨论】:

          • 第一次替换会将两个非“RAI”值连接成一个。可能想要修复它。实际上,所有这些技术都会以错误的方式去除空格
          • 那么没有正则表达式可以在不执行 3 replaceAll 的情况下解决问题吗?像\s?RAI\s? 这样的东西不起作用?
          【解决方案7】:

          使用

          String wordToReplace = "RAI";
          String regex = "\\s*\\" + wordToReplace + "\\b\\s*";
          str = str.replaceAll(regex, "");
          

          【讨论】:

          • 您可能应该使用“Pattern.quote”转义 wordToReplace
          猜你喜欢
          • 2017-12-04
          • 2022-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-16
          相关资源
          最近更新 更多