简体   繁体   中英

Couldn't remove a single quotes from a string?

This is my string:

String str = "CREATE TABLE `patiant` (  `ID` varchar(45) NOT NULL,  `PATIANT_NAME`...";

I tried using replace and replaceAll methods but didn't work.
Here you can see what I'v tried I don't know why it's not working:

  1. temp = str.replace("'","");
  2. temp = str.replaceAll("(^')","");

How to remove all quotes from the string?
Thanks

All of your quotes are grave accents (`), so you are replacing the wrong type of quote. So try this instead:

temp = str.replaceAll("`","");

The input String contains backticks rather than single quotes so these need to be replaced rather than the latter:

Either

temp = str.replace("`", "");

or

temp = str.replaceAll("`", "");

will perform the replacement correctly

here the code so you can prove rigth now:


public class TestMain {

public TestMain() {
    // TODO Auto-generated constructor stub
}


public static void main(String[] args) {

    String str = "CREATE TABLE `patiant` (  `ID` varchar(45) NOT NULL,  `PATIANT_NAME`...";
    String new_string_remplace = str.replaceAll("`",""); 
    System.out.println(new_string_remplace);
    //RESULT: CREATE TABLE patiant (  ID varchar(45) NOT NULL,  PATIANT_NAME...

}

}

Note: the replace String java.lang.String.replaceAll(String regex, String replacement).

Replaces each substring of this string that matches the given regular expression with the given replacement.

An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM