Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace all asterisks?

Tags:

java

regex

In Java, I want to replace all * characters with \*.

Example: Text: select * from blah

Result: select \\* from blah

public static void main(String[] args) {
    String test = "select * from blah";
    test = test.replaceAll("*", "\\*");
    System.out.println(test);
}

This does not work, nor does adding a escape backslash.

like image 217
Tim Avatar asked Oct 05 '10 00:10

Tim


People also ask

How do I change an asterisk in Word?

Press Ctrl+H to open the Find and Replace dialog box. Click the More button to show more find/replace options. Select the Use wildcards checkbox. Click Find Next to find the first instance, then Replace to replace the multiple asterisks with a paragraph mark.


1 Answers

I figured it out

    String test = "select * from blah *asdf";
    test = test.replaceAll("\\*", "\\\\*");
    System.out.println(test);
like image 99
Tim Avatar answered Oct 09 '22 15:10

Tim