C# 程式可以將字元列表轉換為字串
首先,宣告字元陣列,並設定每個字元的值 −
char[] ch = new char[5]; ch[0] = 'H'; ch[1] = 'e'; ch[2] = 'l'; ch[3] = 'l'; ch[4] = 'o';
現在,使用字串類建構函式,並使用上述字元陣列建立一個新字串 −
string myChar = new string(ch);
示例
讓我們來看看 C# 中將字元列表轉換為字串的程式碼。
using System; namespace Demo { class MyApplication { static void Main(string[] args) { char[] ch = new char[5]; ch[0] = 'H'; ch[1] = 'e'; ch[2] = 'l'; ch[3] = 'l'; ch[4] = 'o'; string myChar = new string(ch); Console.WriteLine("Converted to string = {0}", myChar); } } }
輸出
Converted to string = Hello
廣告