C#如何将string转为char[]
答案:4 悬赏:70 手机版
解决时间 2021-04-06 07:24
- 提问者网友:骑士
- 2021-04-05 12:23
C#如何将string转为char[]
最佳答案
- 五星知识达人网友:毛毛
- 2021-04-05 13:06
使用string的方法ToCharArray就可以把string转换成char[]数据
MSDN对ToCharArray如下说明
public char[] ToCharArray()
返回值
Type: System.Char[]
元素为此实例的各字符的 Unicode 字符数组。 如果此实例是空字符串,则返回的数组为空且长度为零。
例子:
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("
count = {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-
// --
MSDN对ToCharArray如下说明
public char[] ToCharArray()
返回值
Type: System.Char[]
元素为此实例的各字符的 Unicode 字符数组。 如果此实例是空字符串,则返回的数组为空且长度为零。
例子:
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("
count = {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-
// --
全部回答
- 1楼网友:独钓一江月
- 2021-04-05 16:01
string ss = "abcdefg";
char[] cc = ss.ToCharArray();
char[] cc = ss.ToCharArray();
- 2楼网友:傲气稳了全场
- 2021-04-05 14:24
string st = @"C:\Users\i-rons\Desktop\C#文?.txt";
char[] a=new char[]{};
a=st.ToArray();
char[] a=new char[]{};
a=st.ToArray();
- 3楼网友:西风乍起
- 2021-04-05 13:46
char[] cs = yourstring.ToCharArray();
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯