Srivastava
Srivastava

Reputation: 3578

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!

Upvotes: 0

Views: 1348

Answers (2)

Arpit
Arpit

Reputation: 12797

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);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502985

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.

Upvotes: 3

Related Questions