本文整理汇总了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);
}
}
输出:
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]);
}
}
输出:
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);
}
}
}
}
输出:
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);
}
}