Java中,想替换字符串中除指定特殊子字符串外的所有字符,可以使用正则表达式等方法来实现。本文主要介绍Java中字符串替换,指定的子字符串不做替换,除指定的子字符串,字符串中其它字符进行替换。

实现的效果示例

plusOut("12xy34", "xy") → "++xy++"
plusOut("12xy34", "1") → "1+++++"
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy"
plusOut("abXYabcXYZ", "ab") → "ab++ab++++"
plusOut("abXYabcXYZ", "abc") → "++++abc+++"
plusOut("abXYabcXYZ", "XY") → "++XY+++XY+"
plusOut("abXYxyzXYZ", "XYZ") → "+++++++XYZ"
plusOut("--++ab", "++") → "++++++"
plusOut("aaxxxxbb", "xx") → "++xxxx++"
plusOut("123123", "3") → "++3++3"

1、使用StringBuilder和for循环实现

public class Main {
  public static String plusOut(String str, String word) {
    StringBuilder out = new StringBuilder(str);
    for (int i = 0; i < out.length();  ) {
        if (!str.startsWith(word,  i))
            out.setCharAt(i++,  '+');
        else 
            i += word.length(); 
    }
    return out.toString(); 
 }
  public static void main(String[] args) {
    System.out.println(plusOut("12xy34",  "xy")); // 输出: "++xy++"
    System.out.println(plusOut("12xy34",  "1")); // 输出: "1+++++"
    System.out.println(plusOut("12xy34xyabcxy",  "xy")); // 输出: "++xy++xy+++xy"
    System.out.println(plusOut("abXYabcXYZ",  "ab")); // 输出: "ab++ab++++"
    System.out.println(plusOut("abXYabcXYZ",  "abc")); // 输出: "++++abc+++"
    System.out.println(plusOut("abXYabcXYZ",  "XY")); // 输出: "++XY+++XY+"
    System.out.println(plusOut("abXYxyzXYZ",  "XYZ")); // 输出: "+++++++XYZ"
    System.out.println(plusOut("--++ab",  "++")); // 输出: "++++++"
    System.out.println(plusOut("aaxxxxbb",  "xx")); // 输出: "++xxxx++"
    System.out.println(plusOut("123123",  "3")); // 输出: "++3++3"
  }
}

2、使用replaceAll和正则表达式实现

public class Main {
  public static String plusOut(String str, String word) {
      return str.replaceAll(java.util.regex.Pattern.quote(word), "@"   ).replaceAll("[^@]", "+").replaceAll("@", word);
  }
  public static void main(String[] args) {
    
    System.out.println(plusOut("12xy34",  "xy")); // 输出: "++xy++"
    System.out.println(plusOut("12xy34",  "1")); // 输出: "1+++++"
    System.out.println(plusOut("12xy34xyabcxy",  "xy")); // 输出: "++xy++xy+++xy"
    System.out.println(plusOut("abXYabcXYZ",  "ab")); // 输出: "ab++ab++++"
    System.out.println(plusOut("abXYabcXYZ",  "abc")); // 输出: "++++abc+++"
    System.out.println(plusOut("abXYabcXYZ",  "XY")); // 输出: "++XY+++XY+"
    System.out.println(plusOut("abXYxyzXYZ",  "XYZ")); // 输出: "+++++++XYZ"
    System.out.println(plusOut("--++ab",  "++")); // 输出: "++++++"
    System.out.println(plusOut("aaxxxxbb",  "xx")); // 输出: "++xxxx++"
    System.out.println(plusOut("123123",  "3")); // 输出: "++3++3"
    
  }
}

3、使用正则表达式 \G实现

public class Main {
  
  public static String plusOut(String str, String word) {
    word = java.util.regex.Pattern.quote(word);
    return str.replaceAll("\\G((?:" + word + ")*+).", "$1+");
  }
  
  public static void main(String[] args) {
    
    System.out.println(plusOut("12xy34",  "xy")); // 输出: "++xy++"
    System.out.println(plusOut("12xy34",  "1")); // 输出: "1+++++"
    System.out.println(plusOut("12xy34xyabcxy",  "xy")); // 输出: "++xy++xy+++xy"
    System.out.println(plusOut("abXYabcXYZ",  "ab")); // 输出: "ab++ab++++"
    System.out.println(plusOut("abXYabcXYZ",  "abc")); // 输出: "++++abc+++"
    System.out.println(plusOut("abXYabcXYZ",  "XY")); // 输出: "++XY+++XY+"
    System.out.println(plusOut("abXYxyzXYZ",  "XYZ")); // 输出: "+++++++XYZ"
    System.out.println(plusOut("--++ab",  "++")); // 输出: "++++++"
    System.out.println(plusOut("aaxxxxbb",  "xx")); // 输出: "++xxxx++"
    System.out.println(plusOut("123123",  "3")); // 输出: "++3++3"
    
  }
}

4、使用stream实现

import java.util.Arrays;  
import java.util.stream.*;

public class Main {
  
  public static String plusOut(String str, String word) {
    return String.join(word, 
      Arrays.stream(str.split(java.util.regex.Pattern.quote(word), -1))
        .map((String s) -> s.replaceAll("(?s:.)", "+"))
        .collect(Collectors.toList()));
  }
  
  public static void main(String[] args) {
    
    System.out.println(plusOut("12xy34",  "xy")); // 输出: "++xy++"
    System.out.println(plusOut("12xy34",  "1")); // 输出: "1+++++"
    System.out.println(plusOut("12xy34xyabcxy",  "xy")); // 输出: "++xy++xy+++xy"
    System.out.println(plusOut("abXYabcXYZ",  "ab")); // 输出: "ab++ab++++"
    System.out.println(plusOut("abXYabcXYZ",  "abc")); // 输出: "++++abc+++"
    System.out.println(plusOut("abXYabcXYZ",  "XY")); // 输出: "++XY+++XY+"
    System.out.println(plusOut("abXYxyzXYZ",  "XYZ")); // 输出: "+++++++XYZ"
    System.out.println(plusOut("--++ab",  "++")); // 输出: "++++++"
    System.out.println(plusOut("aaxxxxbb",  "xx")); // 输出: "++xxxx++"
    System.out.println(plusOut("123123",  "3")); // 输出: "++3++3"
    
  }
}

推荐文档

相关文档

大家感兴趣的内容

随机列表