給定一個字符,任務是在 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
相關用法
- C# String轉Character Array用法及代碼示例
- C# Convert.ToSingle(String, IFormatProvider)用法及代碼示例
- C# Convert.ToString(String, IFormatProvider)用法及代碼示例
- C# Convert.FromBase64String(String)用法及代碼示例
- C# Convert.ToBoolean(String, IFormatProvider)用法及代碼示例
- C# Convert.ToByte(String, IFormatProvider)用法及代碼示例
- C# Convert.ToDateTime(String, IFormatProvider)用法及代碼示例
- C# Convert.ToChar(String, IFormatProvider)用法及代碼示例
- C# Convert.ToDecimal(String, IFormatProvider)用法及代碼示例
- C# Convert.ToInt32(String, IFormatProvider)用法及代碼示例
- C# Convert.ToDouble(String, IFormatProvider)用法及代碼示例
- C# Convert.ToInt16(String, IFormatProvider)用法及代碼示例
- C# Convert.ToSByte(String, IFormatProvider)用法及代碼示例
- C# Convert.ToInt64(String, IFormatProvider)用法及代碼示例
- C# Convert.ToUInt16(String, IFormatProvider)用法及代碼示例
- C# Convert.ToUInt32(String, IFormatProvider)用法及代碼示例
- C# Convert.ToUInt64(String, IFormatProvider)用法及代碼示例
- C# File.Replace(String, String, String)用法及代碼示例
- C# File.Replace(String, String, String, Boolean)用法及代碼示例
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Convert a Character to the String in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。