當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# Character轉String用法及代碼示例

給定一個字符,任務是在 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

相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Convert a Character to the String in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。