首页  >  文章  >  后端开发  >  C#程序将字符列表转换为字符串

C#程序将字符列表转换为字符串

PHPz
PHPz转载
2023-08-22 11:08:38845浏览

C#程序将字符列表转换为字符串

首先,声明字符数组并设置每个字符的值 −

char[] ch = new char[5];

ch[0] = 'H';
ch[1] = 'e';
ch[2] = 'l';
ch[3] = 'l';
ch[4] = 'o';

Now, use the string class constructor and create a new string from the above array of characters −

string myChar = new string(ch);

Example

Let us see the code to convert a list of characters to string in C#.

Live Demo

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);
      }
   }
}

Output

Converted to string = Hello

以上是C#程序将字符列表转换为字符串的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除