PC Review


Reply
Thread Tools Rate Thread

Comparing string to ascii val

 
 
glenn
Guest
Posts: n/a
 
      14th Feb 2005
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


 
Reply With Quote
 
 
 
 
Chris, Master of All Things Insignificant
Guest
Posts: n/a
 
      14th Feb 2005
One way:

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

Chris

"glenn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th Feb 2005
glenn <(E-Mail Removed)> wrote:
> 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)

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th Feb 2005
Chris, Master of All Things Insignificant <chris@No_Spam_Please.com>
wrote:
> One way:
>
> if ( ASC(myvar.substr( 1,1 )) == 65 )
> {
> ...
> }


Except that isn't C#

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
glenn
Guest
Posts: n/a
 
      15th Feb 2005
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 [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> glenn <(E-Mail Removed)> wrote:
> > 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)
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Chad Z. Hower aka Kudzu
Guest
Posts: n/a
 
      15th Feb 2005
"glenn" <(E-Mail Removed)> wrote in news:eFo7YrwEFHA.3120
@TK2MSFTNGP12.phx.gbl:
> 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/
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Feb 2005
glenn <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
glenn
Guest
Posts: n/a
 
      15th Feb 2005
Thanks...

glenn

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> glenn <(E-Mail Removed)> wrote:
> > 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)
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Jay B. Harlow [MVP - Outlook] Microsoft C# .NET 7 1st Aug 2003 06:03 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Mikael Jansson Microsoft C# .NET 0 31st Jul 2003 08:42 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Jon Skeet Microsoft C# .NET 0 31st Jul 2003 08:38 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Frank Oquendo Microsoft C# .NET 0 31st Jul 2003 08:36 PM
Re: ASCII Low Values (Ascii 0) to a string Manfred Suttorp Microsoft Dot NET Framework 2 25th Jul 2003 02:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:14 AM.