System.String.ToCharArray 方法

方法描述

将此实例中的字符复制到 Unicode 字符数组。

语法定义(C# System.String.ToCharArray 方法 的用法)

public char[] ToCharArray()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Char[] 元素为此实例的各字符的 Unicode 字符数组。 如果此实例是空字符串,则返回的数组为空且长度为零。

提示和注释

System.String.ToCharArray 方法例子

然后,对该数组应用 Split 方法。

using System;

public class StringSplit2 
{
   public static void Main() 
   {
      string delimStr = " ,.:";
	   char [] delimiter = delimStr.ToCharArray();
      string words = "one two,three:four.";
      string [] split = null;

	   Console.WriteLine("The delimiters are -{0}-", delimStr);
	   for (int x = 1; x <= 5; x++) 
      {
	      split = words.Split(delimiter, x);
         Console.WriteLine("\ncount = {0,2} ..............", x);
	       foreach (string s in split) 
          {
             Console.WriteLine("-{0}-", s);
  	       }
	   }
   }
}
// The example displays the following output:
//       The delimiters are - ,.:-
//       count =  1 ..............
//       -one two,three:four.-
//       count =  2 ..............
//       -one-
//       -two,three:four.-
//       count =  3 ..............
//       -one-
//       -two-
//       -three:four.-
//       count =  4 ..............
//       -one-
//       -two-
//       -three-
//       -four.-
//       count =  5 ..............
//       -one-
//       -two-
//       -three-
//       -four-
//       --

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。