📜  在C#中将字符转换为字符串

📅  最后修改于: 2021-05-29 18:51:31             🧑  作者: Mango

给定一个字符,任务就是将字符入C#中的字符串。

例子:

Input :  X = 'a'
Output : string S = "a"

Input :  X = 'A'
Output : string S = "A"

方法:我们的想法是使用ToString()方法,参数是字符并返回字符串的Unicode字符转换为字符串。

// convert the character x
// to string s
public string ToString(IFormatProvider provider);
C#
// C# program to character to the string
using System;
  
public class GFG{
      
    static string getString(char x) 
    {
        // Char.ToString() is a System.Char 
        // struct method which is used 
        // to convert the value of this
        // instance to its equivalent
        // string representation
        string str = x.ToString();
          
        return str;
    }
    
    static void Main(string[] args)
    {
        char chr = 'A';
        Console.WriteLine("Type of "+ chr +" : " + chr.GetType());
          
        string str = getString(chr);
        Console.WriteLine("Type of "+ str +" : " + str.GetType());
  
    }
}


输出:

Type of A : System.Char
Type of A : System.String