string startswith or endswith a double quote?

Z

zacks

I need to check the current value of a string value to see if it
either starts with or ends with a double quote character, as in
"123" (where the quotes are actual contens of the string). I thought a
quotation character inside of a string is specified with two double
quote characters. But when I code something like:

string myString;

if (myString.StartsWith(""""))
{
}

A syntax error of "Expecting )" is given.

How can I do this in C#.NET?
 
Z

zacks

I need to check the current value of a string value to see if it
either starts with or ends with a double quote character, as in
"123" (where the quotes are actual contens of the string). I thought a
quotation character inside of a string is specified with two double
quote characters. But when I code something like:

string myString;

if (myString.StartsWith(""""))
{

}

A syntax error of "Expecting )" is given.

How can I do this in C#.NET?

Sorry for the wasted bandwidth, I figured this out minutes after
posting this. I had forgotten how C# likes to escape special
characters. It is done with:

string myString;

myString = "\"123\"";
if (myString.StartsWith("\""))
MessageBox.Show("YES");
 
D

Dave Anson

(e-mail address removed) wrote in @n39g2000hsh.googlegroups.com:
I need to check the current value of a string value to see if it
either starts with or ends with a double quote character, as in
"123" (where the quotes are actual contens of the string). I thought a
quotation character inside of a string is specified with two double
quote characters. But when I code something like:

string myString;

if (myString.StartsWith(""""))
{
}

A syntax error of "Expecting )" is given.

How can I do this in C#.NET?


if (myString.StartsWith("\""))
{
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Sorry for the wasted bandwidth, I figured this out minutes after
posting this. I had forgotten how C# likes to escape special
characters. It is done with:

string myString;

myString = "\"123\"";
if (myString.StartsWith("\""))
MessageBox.Show("YES");

Tip: If you know for certain that the length of the string is always at
least one, you can just check the first character:

if (myString[0] == '"')
 
P

Peter Duniho

Göran Andersson said:
Tip: If you know for certain that the length of the string is always at
least one, you can just check the first character:

if (myString[0] == '"')

For that matter, if the length is also always at least one:

if (myString[myString.Length - 1] == '"')

That said, since you could also do the equivalent using the Substring()
method, even for search strings larger than a single character, it seems
obvious to me that the main reason StartsWith() and EndsWidth() even
exist is more for convenience and readability than anything else.

And IMHO, that motivation for using those methods doesn't go away for
strings that are always at least one character long. :)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top