473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing string to ascii val

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
Nov 16 '05 #1
7 2095
One way:

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

Chris

"glenn" <gh******@softeksoftware.com> wrote in message
news:OG**************@TK2MSFTNGP15.phx.gbl...
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

Nov 16 '05 #2
glenn <gh******@softeksoftware.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
glenn <gh******@softeksoftware.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
"glenn" <gh******@softeksoftware.com> 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/
Nov 16 '05 #6
glenn <gh******@softeksoftware.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Thanks...

glenn

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
glenn <gh******@softeksoftware.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
4
by: http://www.visual-basic-data-mining.net/forum | last post by:
Hi Does anyone know how to stay connected to the server and at the same time i can pass the string to and from the module to the form..... What I want: I put the connection at the...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
15
by: luc.saffre | last post by:
Hello, here is something that surprises me. #coding: iso-8859-1 s1=u"Frau Müller machte große Augen" s2="Frau Müller machte große Augen" if s1 == s2: pass
25
by: galapogos | last post by:
Hi, I'm trying to compare an array of unsigned chars(basically just data without any context) with a constant, and I'm not sure how to do that. Say my array is array and I want to compare it with...
13
by: Eps | last post by:
Hi there, I believe all strings in .net are unicode by default, I am looking for a way to remove all non ascii characters from a string (or optionally replace them). There is an article on...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.