How can I add space between bracket and single quotation mark in java?

Viewed 132

I am trying to convert the following string

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

to

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

My code:

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 Answers
    string01 = testString.replaceAll("\\('", "\\(  ");
    string01 = testString.replaceAll("\\)", "  \\)");
    string01 = testString.replaceAll(",", ", ");

Strings are immutable. You can't keep operating on the original string as a new string is returned from each replaceAll() statement.

The code should be:

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

Also, your first replaceAll(...) statement was incorrect, since you missed the ' in the replacement string.

The error was mainly that you did not use string01 for the second and third replacement, but the original testString. (replace(All/First) will not change the string itself, but yield a new string). So the first and second replacements are lost.

Then you do not need regular expression replaces. You can write all like:

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

You can use the regex ('(?=\\))|\\,|\\() and capture group reference $1 with replaceAll() method :

String testString = "Insert into table01 values('abcd01','abcd02','abcd03')";
String string01 = testString.replaceAll("('(?=\\))|\\,|\\()", "$1 ");
System.out.println(string01);
  • (...) : capture group
  • $1 : capture group reference
  • | : OR operator
  • (?=...) : positive lookahead to capture ' before )

Output:

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

If you are having trouble with regular expressions, do consider going back to the basics.

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);
    }
}

You might also use lookarounds to find the specific positions, and in the replacement use a single space.

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

See the positions in this regex demo or a Java demo.

  • (?<=\(|',)(?=') Positive lookbehind assertion, if at the left is either ( or ', and at the right is '
  • | Or
  • (?<=')(?=\)) Positive lookbehind assertions, if at the left is ' and at the right is )

Example

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

Output

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