Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string starts with ' " ' (double quotes)? [duplicate]

Tags:

regex

Every time I write " my compiler assumes I am trying to write a String. Instead I want my method to tell me if the incoming string starts with a double quote "" Ex:

String n;
if(n==n.startsWith(" " " ));

doesn't work

Any suggestions??

like image 711
user4557512 Avatar asked Oct 14 '25 03:10

user4557512


1 Answers

You have to escape double quotes in string! If you do it like this: " " ", string ends on second quotation mark. If in Java, you code should be like:

String n;
if(n.startsWith("\""))
{
    // execute if true
}

Since you are matching just first character, you don't need to use such sophisticated tool as regular expressions:

String n;
if (n.charAt(0)=="\"")
{
    // execute if true
}

BUT. You should make sure if string is not empty. Just for safety:

String n;
    if (n.getText()!=null 
        && !n.getText().isEmpty() 
        && n.charAt(0)=="\"")
    {
        // execute if true
    }

PS: space is a character.

PSS: flagged as dublicate.

like image 77
Tymek Avatar answered Oct 17 '25 01:10

Tymek