llegal escape character followed by a space

后端 未结 2 1789
借酒劲吻你
借酒劲吻你 2021-01-28 00:09

I\'m writing a bit of code to run a shell script using process that loads and runs a file in terminal. The problem I\'m having is getting the filename to be recognised by termin

2条回答
  •  旧巷少年郎
    2021-01-28 00:36

    If you want the string to contain an actual backslash you need to escape the backslash. Otherwise javac thinks you're trying to escape space, which doesn't need escaping:

    string = string.replaceAll(" ", "\\ ");
    

    Using this code, the second argument to the method will be a 2-character string: backslash followed by space. I assume that's what you want.

    See section 3.10.6 of the Java Language Specification for more details of character/string literal escape sequences.

提交回复
热议问题