Backslash in java

前端 未结 4 1161
北海茫月
北海茫月 2021-01-21 13:57

I have a problem with strings saved in a database, like this for example: \"311\\315_316\\336_337\". They have only one backslash and this is a problem in java. Wh

相关标签:
4条回答
  • 2021-01-21 14:52

    You could also use Unicode \ = \u005c

    Example: "Folder\u005cSubfolder1\u005cSubfolder2"

    Would result in the string "Folder\Subfolder1\Subfolder2"

    You can see the full table here: http://jrgraphix.net/r/Unicode/0020-007F

    0 讨论(0)
  • 2021-01-21 14:56

    in java, backslash \ has a special meaning. in order to remove it, escape it with another backslash \\

    try this:

    s.replaceAll("\\", "\\\\");
    
    0 讨论(0)
  • 2021-01-21 14:57

    The data in the database is OK, and you don't have to replace anything. String literals, written directly in the Java code, must have their backslash escaped by another backslash:

    String s = "311\\315_316\\336_337";
    System.out.println(s); // prints 311\315_316\336_337
    

    But if you get those values from the database, you don't have anything to do:

    String s = resultSet.getString(1);
    System.out.println(s); // should print 311\315_316\336_337
    
    0 讨论(0)
  • 2021-01-21 14:59

    In java strings, the backslash character is a control character. If you wish to include a literal backslash in a string, you must escape it with another backslash. e.g. "\\"

    0 讨论(0)
提交回复
热议问题