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?
To check if the string has double quotes you can use: text_line. Contains("\""); Here \" will escape the double-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 .
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With