How do I convert a single char to a string?

后端 未结 11 1734
醉梦人生
醉梦人生 2020-12-18 17:24

I\'d like to enumerate a string and instead of it returning chars I\'d like to have the iterative variable be of type string. This pro

相关标签:
11条回答
  • 2020-12-18 18:03

    You have two options. Create a string object or call ToString method.

    String cString = c.ToString();
    String cString2 = new String(c, 1); // second parameter indicates
                                        // how many times it should be repeated
    
    0 讨论(0)
  • 2020-12-18 18:10

    probably isn't possible to have the iterative type be a string

    Sure it is:

    foreach (string str in myString.Select(c => c.ToString())
    {
    ...
    }
    

    Any of the suggestions in the other answers can be substituted for c.ToString(). Probably the most efficient by a small hair is c => new string(c, 1), which is what char.ToString() probably does under the hood.

    0 讨论(0)
  • 2020-12-18 18:12
    String cString = c.ToString();
    
    0 讨论(0)
  • 2020-12-18 18:12

    you can use + with empty string "", please check the below code:

    char a = 'A';
    //a_str is a string, the value of which is "A".
    string a_str = ""+a;
    
    0 讨论(0)
  • 2020-12-18 18:13

    Use the .ToString() Method

    String myString = "Hello, World";
    foreach (Char c in myString)
    {
        String cString = c.ToString(); 
    }
    
    0 讨论(0)
  • 2020-12-18 18:13

    Create a new string from the char.

     String cString = new String(new char[] { c });
    

    or

     String cString = c.ToString();
    
    0 讨论(0)
提交回复
热议问题