How to check asc char code in C#?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I need to check the asc char code of a textarea in C#. I used charCodeAt in js before. However, which attribute should I use in C# and is InnerText correct

[code
int a = textarea.InnerText.Length
for (i = 0; i< a; i++) {
if (textarea.InnerText.charCodeAt(i) > 127 || textarea.InnerText.charCodeAt(i) < 0

// do sth ;
}
}
[/code

Thanks
 
Tom,

Assuming that you have the document object model, and that you can get
the string representing the text, you can just use the indexer on the string
to get the appropriate character. For example:

// A string
string pstrHello = "Hello";

// The second character in the string.
char pchrSecond = pstrHello[1];

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tom said:
Hi,

I need to check the asc char code of a textarea in C#. I used charCodeAt
in js before. However, which attribute should I use in C# and is InnerText
correct?
Code:
int a = textarea.InnerText.Length;
for (i = 0; i< a; i++) {
if (textarea.InnerText.charCodeAt(i) > 127 ||[/QUOTE]
textarea.InnerText.charCodeAt(i) < 0)[QUOTE]
{
// do sth ;
}
}

Thanks
 
Tom,

That code will work. You might want to create a StringBuilder, and
append each character as you add it. When you are done, you can call the
ToString method on the builder to get the resulting string.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tom said:
If I want to check the asc code of each char in a string, do I build a
char array to loop over the whole string to check?
Is the following code ok? I am not sure how to write the textarea's value in a char array format.

int a = textarea.InnerText.Length;
for (i = 0; i< a; i++) {
if (textarea.InnerText > 127 || textarea.InnerText < 0)
{
// do sth ;
}
}

Thanks for help
 

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