How do I convert a string to a char?

Discussion in 'ASP .Net' started by Alan Silver, Jun 20, 2005.

  1. Alan Silver

    Alan Silver Guest

    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>
    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Jun 20, 2005
    #1
    1. Advertising

  2. Alan Silver

    Teemu Keiski Guest

    Teemu Keiski, Jun 20, 2005
    #2
    1. Advertising

  3. Alan Silver

    Mark Rae Guest

    "Alan Silver" <> wrote in message
    news:...

    > 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(...) ?
    Mark Rae, Jun 20, 2005
    #3
  4. 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.

    "Alan Silver" <> wrote in message
    news:...
    > 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>
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
    Kevin Spencer, Jun 20, 2005
    #4
  5. Alan Silver

    Alan Silver Guest

    >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.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Jun 20, 2005
    #5
  6. Alan Silver

    Alan Silver Guest

    >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];
    >


    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Jun 21, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    550
  2. wwj
    Replies:
    24
    Views:
    2,507
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    473
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    582
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,046
    Ian Collins
    May 9, 2006
Loading...

Share This Page