java - 如何在括号和单引号之间添加空格?

时间:2021-05-02 15:00:35

标签: java string

我正在尝试转换以下字符串

Insert into table01 values('abcd01','abcd02','abcd03')

Insert into table01 values( 'abcd01', 'abcd02', 'abcd03' )

我的代码:

package layout;
public class String02 {
    public String Replace01(String testString) {
        String string01 = "";
        string01 = testString.replaceAll("\\('", "\\(  ");
        string01 = testString.replaceAll("\\)", "  \\)");
        string01 = testString.replaceAll(",", ", ");
        return string01;
    }
    public static void main(String[] args) {
        String02 app = new String02();
        String testString = "Insert into table01 values('abcd01','abcd02','abcd03')";
        String s1 = app.Replace01(testString);
        System.out.println(s1);
    }
}

5 个答案:

答案 0 :(得分:5)

    string01 = testString.replaceAll("\\('", "\\(  ");
    string01 = testString.replaceAll("\\)", "  \\)");
    string01 = testString.replaceAll(",", ", ");

字符串是不可变的。您不能继续对原始字符串进行操作,因为每个 replaceAll() 语句都会返回一个新字符串。

代码应该是:

    string01 = testString.replaceAll("\\('", "\\(  '");
    string01 = string01.replaceAll("\\)", "  \\)");
    string01 = string01.replaceAll(",", ", ");

此外,您的第一个 replaceAll(...) 语句不正确,因为您错过了替换字符串中的 '

答案 1 :(得分:3)

错误主要是您没有使用string01进行第二次和第三次替换, 但原来的testString。 (replace(All/First) 不会改变字符串本身,而是产生一个新字符串)。 所以第一次和第二次替换都丢失了。

那么你就不需要正则表达式替换了。你可以这样写:

    string01 = testString.replace("('", "( '")
                         .replace(")", " )")
                         .replace("','", "', '");

答案 2 :(得分:1)

您可以使用正则表达式 ('(?=\\))|\\,|\\() 并通过 $1 方法捕获组引用 replaceAll() :

String testString = "Insert into table01 values('abcd01','abcd02','abcd03')";
String string01 = testString.replaceAll("('(?=\\))|\\,|\\()", "$1 ");
System.out.println(string01);
  • (...) : 捕获组
  • $1 : 捕获组引用
  • | : OR 运算符
  • (?=...) :在 ' 之前捕获 ) 的正向预测

输出:

Insert into table01 values( 'abcd01', 'abcd02', 'abcd03' )

答案 3 :(得分:0)

如果您在使用正则表达式时遇到问题,请考虑回归基础。

public class String02 {
    public String Replace01(String testString) {
        String string01 = "";
        char ch;
        for(int i=0; i<testString.length(); i++){
            ch = testString.charAt(i);
            if(ch=='(' || ch==','){
                if((i+1)<testString.length() && testString.charAt(i+1)!=' '){
                    string01 += ch + " ";
                    continue;
                }
            }else if (ch==')'){
                if((i-1)>=0 && testString.charAt(i-1)!=' '){
                    string01 += " " + ch;
                    continue;
                }
            }
            string01 += ch;
        }
        return string01;
    }
    public static void main(String[] args) {
        String02 app = new String02();
        String testString = "Insert into table01 values('abcd01','abcd02','abcd03')";
        String s1 = app.Replace01(testString);
        System.out.println(s1);
    }
}

答案 4 :(得分:0)

您也可以使用环视来查找特定的位置,并在替换中使用单个空格。

(?<=\(|',)(?=')|(?<=')(?=\))

查看此 regex demoJava demo 中的位置。

  • (?<=\(|',)(?=') 正向后视断言,如果左边是 (',,右边是 '
  • |
  • (?<=')(?=\)) 正向后视断言,如果左边是 ',右边是 )

示例

String s = "Insert into table01 values('abcd01','abcd02','abcd03')";
String result = s.replaceAll("(?<=\\(|',)(?=')|(?<=')(?=\\))", " ");
System.out.println(result);

输出

Insert into table01 values( 'abcd01', 'abcd02', 'abcd03' )