Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string equals quotation marks

Tags:

java

I am trying to check if a string equals quotation marks ("). However, string.equals(""") does not work since it thinks I have an extra quotation mark. How can I check if the string equals quotation marks?

like image 417
user3474775 Avatar asked May 06 '14 00:05

user3474775


People also ask

How do you know if a string has a double quote?

To check if the string has double quotes you can use: text_line. Contains("\""); Here \" will escape the double-quote.

How can you tell if a character is a quote?

If you are dealing with a char then simply do this: c == '"'; If c is equal to the double quote the expression will evaluate to true .


2 Answers

str.equals("\"");

\ is used as an escape character to tell the compiler that the next character is to be interpreted literally. In this case, it causes the " to be interpreted as a character in the string instead of as a ending quotation mark. \" is used to represent ".

To be safer with null strings, you can also do:

"\"".equals(str);

This will return false if str is null instead of throwing a NullPointerException.

like image 188
Anubian Noob Avatar answered Oct 13 '22 06:10

Anubian Noob


Use the escape character \. This lets it know the next character should be read as text and not interpreted by the compiler. string.equals("\"") will work.

like image 39
donutmonger Avatar answered Oct 13 '22 06:10

donutmonger