Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java中的其中一个替换字符串中的相同单词_Java_String - Fatal编程技术网

用java中的其中一个替换字符串中的相同单词

用java中的其中一个替换字符串中的相同单词,java,string,Java,String,我想用其中的一个词来替换相同的词 "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1" 改变这个 "p1 p2 p3 p4 p5 p2 p1" java中有什么方法可以做到这一点吗?您可以使用regex String text = "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1"; System.out.println(text.replaceAll("(\\w+)(\

我想用其中的一个词来替换相同的词

 "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1"
改变这个

 "p1 p2 p3 p4 p5 p2 p1"
java中有什么方法可以做到这一点吗?

您可以使用regex

String text = "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1";
System.out.println(text.replaceAll("(\\w+)(\\s+\\1)+", "$1"));
输出:

p1 p2 p3 p4 p5 p2 p1
  • (\\w+)
    将匹配单个单词,由于有括号,它将被放在组1中
  • (\\s+\\1)+
    \\s+
    中,表示一个或多个空格,
    \\1
    需要与组1相同的匹配。用
    (…)+
    包围它需要它存在一次或多次
  • 作为替换,我们使用了
    $1
    ,它将匹配项存储在组1中(因此我们用第一个单词替换了许多相同的单词)
      您可以使用正则表达式

      String text = "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1";
      System.out.println(text.replaceAll("(\\w+)(\\s+\\1)+", "$1"));
      
      输出:

      p1 p2 p3 p4 p5 p2 p1
      
      • (\\w+)
        将匹配单个单词,由于有括号,它将被放在组1中
      • (\\s+\\1)+
        \\s+
        中,表示一个或多个空格,
        \\1
        需要与组1相同的匹配。用
        (…)+
        包围它需要它存在一次或多次
      • 作为替换,我们使用了
        $1
        ,它将匹配项存储在组1中(因此我们用第一个单词替换了许多相同的单词)
          您可以使用正则表达式

          String text = "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1";
          System.out.println(text.replaceAll("(\\w+)(\\s+\\1)+", "$1"));
          
          输出:

          p1 p2 p3 p4 p5 p2 p1
          
          • (\\w+)
            将匹配单个单词,由于有括号,它将被放在组1中
          • (\\s+\\1)+
            \\s+
            中,表示一个或多个空格,
            \\1
            需要与组1相同的匹配。用
            (…)+
            包围它需要它存在一次或多次
          • 作为替换,我们使用了
            $1
            ,它将匹配项存储在组1中(因此我们用第一个单词替换了许多相同的单词)
              您可以使用正则表达式

              String text = "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1";
              System.out.println(text.replaceAll("(\\w+)(\\s+\\1)+", "$1"));
              
              输出:

              p1 p2 p3 p4 p5 p2 p1
              
              • (\\w+)
                将匹配单个单词,由于有括号,它将被放在组1中
              • (\\s+\\1)+
                \\s+
                中,表示一个或多个空格,
                \\1
                需要与组1相同的匹配。用
                (…)+
                包围它需要它存在一次或多次
              • 作为替换,我们使用了
                $1
                ,它将匹配项存储在组1中(因此我们用第一个单词替换了许多相同的单词)
                  这里有一个解决方案:

                  public static void main(String[] args)
                  {
                      String s = "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1";
                      String[] parts = s.split(" ");
                      ArrayList<String> arr = new ArrayList<String>();
                  
                      arr.add(parts[0]); // Add first item
                  
                      for (int i = 1; i < parts.length; i++) {
                          if (!parts[i - 1].equals(parts[i])) { // Check if last item is different
                              arr.add(parts[i]);
                          }
                      }
                  
                      StringBuilder sb = new StringBuilder();
                  
                      for (String str: arr) { // Creating the new String via StringBuilder
                          sb.append(str);
                          sb.append(" ");
                      }
                  
                      System.out.println(sb.toString());
                  }
                  
                  这里有一个解决方案:

                  public static void main(String[] args)
                  {
                      String s = "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1";
                      String[] parts = s.split(" ");
                      ArrayList<String> arr = new ArrayList<String>();
                  
                      arr.add(parts[0]); // Add first item
                  
                      for (int i = 1; i < parts.length; i++) {
                          if (!parts[i - 1].equals(parts[i])) { // Check if last item is different
                              arr.add(parts[i]);
                          }
                      }
                  
                      StringBuilder sb = new StringBuilder();
                  
                      for (String str: arr) { // Creating the new String via StringBuilder
                          sb.append(str);
                          sb.append(" ");
                      }
                  
                      System.out.println(sb.toString());
                  }
                  
                  这里有一个解决方案:

                  public static void main(String[] args)
                  {
                      String s = "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1";
                      String[] parts = s.split(" ");
                      ArrayList<String> arr = new ArrayList<String>();
                  
                      arr.add(parts[0]); // Add first item
                  
                      for (int i = 1; i < parts.length; i++) {
                          if (!parts[i - 1].equals(parts[i])) { // Check if last item is different
                              arr.add(parts[i]);
                          }
                      }
                  
                      StringBuilder sb = new StringBuilder();
                  
                      for (String str: arr) { // Creating the new String via StringBuilder
                          sb.append(str);
                          sb.append(" ");
                      }
                  
                      System.out.println(sb.toString());
                  }
                  
                  这里有一个解决方案:

                  public static void main(String[] args)
                  {
                      String s = "p1 p2 p2 p2 p2 p3 p3 p4 p5 p5 p5 p5 p5 p2 p2 p1 p1 p1";
                      String[] parts = s.split(" ");
                      ArrayList<String> arr = new ArrayList<String>();
                  
                      arr.add(parts[0]); // Add first item
                  
                      for (int i = 1; i < parts.length; i++) {
                          if (!parts[i - 1].equals(parts[i])) { // Check if last item is different
                              arr.add(parts[i]);
                          }
                      }
                  
                      StringBuilder sb = new StringBuilder();
                  
                      for (String str: arr) { // Creating the new String via StringBuilder
                          sb.append(str);
                          sb.append(" ");
                      }
                  
                      System.out.println(sb.toString());
                  }
                  

                  谢谢很好的解决方案,谢谢!很好的解决方案,谢谢!很好的解决方案,谢谢!很好的解决方案。谢谢你的回答和小建议。使用
                  append(str).append(“”
                  )代替追加
                  str+“”
                  。这样,您就不必连接两个字符串(并使用另一个StringBuilder)。@Pshemo感谢您指出这一点。我不知道为什么我总是忽视这一点。谢谢你的回答和建议。使用
                  append(str).append(“”
                  )代替追加
                  str+“”
                  。这样,您就不必连接两个字符串(并使用另一个StringBuilder)。@Pshemo感谢您指出这一点。我不知道为什么我总是忽视这一点。谢谢你的回答和建议。使用
                  append(str).append(“”
                  )代替追加
                  str+“”
                  。这样,您就不必连接两个字符串(并使用另一个StringBuilder)。@Pshemo感谢您指出这一点。我不知道为什么我总是忽视这一点。谢谢你的回答和建议。使用
                  append(str).append(“”
                  )代替追加
                  str+“”
                  。这样,您就不必连接两个字符串(并使用另一个StringBuilder)。@Pshemo感谢您指出这一点。我不知道为什么我总是忽视这一点。