How can I check if string contains both a Single-quote ('
) and a double-quote ("
), like the one below?
var str = "test'\"";
Enclosing Strings Containing Single and Double Quotes Actually, we can use triple quotes (i.e., triplet of single quotes or triplet double quotes) to represent the strings containing both single and double quotes to eliminate the need of escaping any. >>> print('''She said, "Thank you! It's mine."''')
charAt(test. length()-1) == 34) { System. out. println("Has double quotes"); } 34 in ascii table represents doublequote , hence it works fine.
The use of single quotes [ ' ... ' ] or double quotes [ “...” ] differs with context and geographic location. Conventionally, most English speaking countries use double quotes to mark direct speech and single quotes to mark speech within speech.
The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value.
A quick way to check if the string contains both a single quote and a double quote.
if (str.indexOf('\'') >= 0 && str.indexOf('"') >= 0) {
//do something
}
edit: if the character is in the first position, indexOf will return zero.
I'm guessing you want something like /['||"]/.test(str);
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