当前位置: 首页>>代码示例>>C#>>正文


C# String.ToCharArray方法代码示例

本文整理汇总了C#中System.String.ToCharArray方法的典型用法代码示例。如果您正苦于以下问题:C# String.ToCharArray方法的具体用法?C# String.ToCharArray怎么用?C# String.ToCharArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.String的用法示例。


在下文中一共展示了String.ToCharArray方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

// Sample for String.ToCharArray(Int32, Int32)
using System;

class Sample {
    public static void Main() {
    string str = "012wxyz789";
    char[] arr;

    arr = str.ToCharArray(3, 4);
    Console.Write("The letters in '{0}' are: '", str);
    Console.Write(arr);
    Console.WriteLine("'");
    Console.WriteLine("Each letter in '{0}' is:", str);
    foreach (char c in arr)
        Console.WriteLine(c);
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源:String.ToCharArray

输出:

The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z

示例2: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      String s = "AaBbCcDd";
      var chars = s.ToCharArray();
      Console.WriteLine("Original string: {0}", s);
      Console.WriteLine("Character array:");
      for (int ctr = 0; ctr < chars.Length; ctr++)
         Console.WriteLine("   {0}: {1}", ctr, chars[ctr]);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:String.ToCharArray

输出:

Original string: AaBbCcDd
Character array:
0: A
1: a
2: B
3: b
4: C
5: c
6: D
7: d

示例3: Main

//引入命名空间
using System;

public class Example
{
   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 (var s in split) {
             Console.WriteLine("-{0}-", s);
         }
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:22,代码来源:String.ToCharArray

输出:

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-
--

示例4: Main

//引入命名空间
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

public class MainClass
{
    public static void Main()
    {
        string s = "Hello, World.";

        char[] c1 = s.ToCharArray();
        char[] c2 = s.ToCharArray(0, 5);
    }

}
开发者ID:C#程序员,项目名称:System,代码行数:18,代码来源:String.ToCharArray


注:本文中的System.String.ToCharArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。