How do I convert a string to a char?

A

Alan Silver

Hello,

<dumb question>

I am trying to do something similar to the following...

string str = "Hello";
char c = (char)str.Substring(2,1);

but the compiler complains that it cannot convert a string to a char.

How do I do this? I know that c will only be one character long, so it
can be converted to a char, but the compiler doesn't know that.

If it's of any help, I'm converting it to a char in order to get the
ASCII value, so if there's an easier way of converting a one-character
substring of a string to an int, please tell me.

Thanks

</dumb question>
 
M

Mark Rae

string str = "Hello";
char c = (char)str.Substring(2,1);

but the compiler complains that it cannot convert a string to a char.

Have you tried Convert.ToChar(...) ?
 
K

Kevin Spencer

Hi Alan,

First, remember that a string is an array of char. The string object is a
wrapper for an array of char. So, you can simply do the following:

string str = "Hello";
char c = str[2]; // c = 'l'

Then you can cast the char as an int with the following:

int i = (int)c;

Or, to get really elegant,

int i = (int)"Hello"[2];

- or -

int i = (int)str[2];

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
A

Alan Silver

Hi,
what if you try like this:

char c=str[2];

Thanks, I discovered that just after posting!!

I have found an infallible way to get the answers you need. First search
the archives and get frustrated that you can't find it, then post a
message here and IMMEDIATELY go and search the archives again. Doesn't
matter if you use the same keywords as before, you're guaranteed to find
the answer straight away!!

Thanks for the reply.
 
A

Alan Silver

Hi Alan,

Hi Kevin,

Thanks for the reply. As I mentioned in another reply (my own to
myself!!), I discovered this shortly after posting.

I think I am going to investigate the quantum effects of posting a
question in Usenet on the probability of finding the answer within a
specified number of picoseconds after the message has been sent. My
observations (which according to the laws of quantum physics must bias
the effect) indicate that there is a strong correlation here.

I could write a PhD thesis on this, then sell it to Google and retire on
the profits!! Ha ha.

Ta ra
First, remember that a string is an array of char. The string object is a
wrapper for an array of char. So, you can simply do the following:

string str = "Hello";
char c = str[2]; // c = 'l'

Then you can cast the char as an int with the following:

int i = (int)c;

Or, to get really elegant,

int i = (int)"Hello"[2];

- or -

int i = (int)str[2];
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top