Comparing string to ascii val

  • Thread starter Thread starter glenn
  • Start date Start date
G

glenn

Can anyone tell me in C# how to take a substr of a variable and compare it
to a literal ascii number.

ie:

if ( myvar.substr( 1,1 ) == 65 )
{
....
}

thanks,

glenn
 
One way:

if ( ASC(myvar.substr( 1,1 )) == 65 )
{
...
}

Chris
 
glenn said:
Can anyone tell me in C# how to take a substr of a variable and compare it
to a literal ascii number.

ie:

if ( myvar.substr( 1,1 ) == 65 )
{
...
}

Is substr 1-based? If so:

if (myvar[0]=='A')
or
if (myvar[0]==65)
 
Thanks guys. What about this, will this also work?

Convert.ToChar( myvar.substr( 1, 1) ) == 65

which is the best method? as far as the fastest executing?


thanks,

glenn

Jon Skeet said:
glenn said:
Can anyone tell me in C# how to take a substr of a variable and compare it
to a literal ascii number.

ie:

if ( myvar.substr( 1,1 ) == 65 )
{
...
}

Is substr 1-based? If so:

if (myvar[0]=='A')
or
if (myvar[0]==65)
 
glenn said:
Thanks guys. What about this, will this also work?

Convert.ToChar( myvar.substr( 1, 1) ) == 65

which is the best method? as far as the fastest executing?

Offhand yes - but its very innefficient. Jon's method is much simpler and
thus faster.



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
glenn said:
Thanks guys. What about this, will this also work?

Convert.ToChar( myvar.substr( 1, 1) ) == 65

No, because there is no "substr" method. You can use Substring, but
that seems pretty pointless.
which is the best method? as far as the fastest executing?

Using the indexer is faster, but more importantly it's clearer. Chances
are it's not going to be the bottleneck anyway.
 
Thanks...

glenn

Jon Skeet said:
glenn said:
Can anyone tell me in C# how to take a substr of a variable and compare it
to a literal ascii number.

ie:

if ( myvar.substr( 1,1 ) == 65 )
{
...
}

Is substr 1-based? If so:

if (myvar[0]=='A')
or
if (myvar[0]==65)
 

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

Back
Top