简体   繁体   中英

Invalid Escape Sequence in Java + Selenium Web Driver

I need to take a string with values as

String s = "Patient first name must contain at least one letter(a-zA-Z).
May contain numbers, ,(comma), -(dash), '(apostrophe),
/(forward-slash), \(backslash), &(ampersand) or .(period)"

But when I take it in Eclipse, it says to me Invalid Escape Sequence. Can anyone help me here?

Thanks in advance!

Well you've got two problems:

  • Backslashes need to be escaped in Java string literals
  • You can't have multi-line string literals in Java

So you probably want something like:

String s = "Patient first name must contain at least one letter(a-zA-Z)."
    + " May contain numbers, ,(comma), -(dash), '(apostrophe),"
    + " /(forward-slash), \\(backslash), &(ampersand) or .(period)";

... but possibly with line breaks as well. (It's not clear.)

See sections 3.10.5 and 3.10.6 of the Java Language Specification to see what's valid in a string literal.

Try this:

String s = "Patient first name must contain at least one letter(a-zA-Z).May contain numbers, ,(comma), -(dash), '(apostrophe),/(forward-slash), \\(backslash), &(ampersand) or .(period)";//no multiline. and escape backslash \ 
System.out.println(s);

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