locked
how to convert string to char or vice versa

    Question

  • Hi expert,
    what c# code that i can use to convert char to string or vice versa?

    thanks in advance.
    Tuesday, December 05, 2006 1:21 PM

Answers

  • Alternatively you can do:

    char myChar = 'X';

    string myString = new string(myChar,1);
    or
    string myString = myChar.ToString()

    and

    char myChar = myString[0];

    (If you convert a string to a char, you have to choose one of its characters to convert, via array indexing. Using Convert.ToChar(string) always uses the first char in the string.)




    Tuesday, December 05, 2006 1:33 PM
  • you can use this to convert string to a char:

    char[] myChar = theString.ToCharArray();

     

    Thursday, December 07, 2006 2:54 PM

All replies

  • You can use the convert function to convert it.

    Convert.ToString(<character>);

    Convert.ToChar(<string>);

    • Proposed as answer by spk73 Wednesday, December 15, 2010 1:27 PM
    Tuesday, December 05, 2006 1:25 PM
  • Alternatively you can do:

    char myChar = 'X';

    string myString = new string(myChar,1);
    or
    string myString = myChar.ToString()

    and

    char myChar = myString[0];

    (If you convert a string to a char, you have to choose one of its characters to convert, via array indexing. Using Convert.ToChar(string) always uses the first char in the string.)




    Tuesday, December 05, 2006 1:33 PM
  • you can use this to convert string to a char:

    char[] myChar = theString.ToCharArray();

     

    Thursday, December 07, 2006 2:54 PM
  • stringtext="g";

    charchartext=text.ToCharArray()[0];

       

    Thursday, December 13, 2012 7:51 PM