Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串拆分为单个字符的字符串数组_C#_String_Split - Fatal编程技术网

C# 将字符串拆分为单个字符的字符串数组

C# 将字符串拆分为单个字符的字符串数组,c#,string,split,C#,String,Split,我想做一些简单的事情,比如把这个测试变成 我真的需要做点什么吗 test = "this is a test".Select(x => x.ToString()).ToArray(); 编辑:为了澄清,我不想要一个字符数组,理想情况下我想要一个字符串数组。除了我认为有一种更简单的方法外,我并不认为上面的代码有任何错误。您可以在代码中使用每个字符,然后将其视为字符串 下面是一个例子: foreach (char c in s.ToCharArray()) Debug

我想做一些简单的事情,比如把这个测试变成

我真的需要做点什么吗

test = "this is a test".Select(x => x.ToString()).ToArray();
编辑:为了澄清,我不想要一个字符数组,理想情况下我想要一个字符串数组。除了我认为有一种更简单的方法外,我并不认为上面的代码有任何错误。

您可以在代码中使用每个字符,然后将其视为字符串

下面是一个例子:

    foreach (char c in s.ToCharArray())
        Debug.Log("one character ... " +c);

我相信这就是你想要的:

char[] characters = "this is a test".ToCharArray();

C中的字符串已经有一个字符索引器

string test = "this is a test";
Console.WriteLine(test[0]);
而且

if(test[0] == 't')
  Console.WriteLine("The first letter is 't'");
这也行

Console.WriteLine("this is a test"[0]);
而这个

foreach (char c in "this is a test")
  Console.WriteLine(c);
编辑:

我注意到这个问题是关于char[]数组的。如果必须使用string[]数组,请按照以下方式在c中的每个字符处拆分字符串:

string[] test = Regex.Split("this is a test", string.Empty);

foreach (string s in test)
{
  Console.WriteLine(s);
}
试试这个:

var charArray = "this is a test".ToCharArray().Select(c=>c.ToString());

很可能您正在寻找该方法。但是,如您在文章中所述,如果需要字符串[],则需要做更多的工作

    string str = "this is a test.";
    char[] charArray = str.ToCharArray();
    string[] strArray = str.Select(x => x.ToString()).ToArray();
编辑:如果您担心转换的简洁性,我建议您将其转换为扩展方法

public static class StringExtensions
{
    public static string[] ToStringArray(this string s)
    {
        if (string.IsNullOrEmpty(s))
            return null;

        return s.Select(x => x.ToString()).ToArray();
    }
} 
简单!! 一行:

 var res = test.Select(x => new string(x, 1)).ToArray();

将消息转换为字符数组,然后使用for循环将其更改为字符串

string message = "This Is A Test";
string[] result = new string[message.Length];
char[] temp = new char[message.Length];

temp = message.ToCharArray();

for (int i = 0; i < message.Length - 1; i++)
{
     result[i] = Convert.ToString(temp[i]);
}
结果:


您真的希望每个字符都是字符串吗?您的代码var test=这是一个测试,有什么问题吗?Selectx=>x.ToString.ToArray+因为我在发布答案时漏掉了一个明显的要点。嗯。为什么我没有想到呢?它们有一个char索引器,它们实现了IEnumerable,但实际上它们不是char[]或IList.true。它们是string类型的,但关键是开发人员不需要使用tocharray或任何类型的字符串[]转换来访问单个字符@现在让我走同一条路。有趣的是,Regex.Split将字符串视为在第一个字符之前和最后一个字符之后有一个空字符串。虽然这不是我所要求的,但我想我开始意识到我所要求的并不是理由。@walkereno你真正想要什么?“a-1?”沃克诺,这就是我回答这个问题的原因。我想不出你想要一个单字符串数组的原因。不过别担心。@L.B,对不起,我更新了帖子。没有意识到我需要什么。@BrandonMoretz我刚刚投票了,所以你的代表从一个非常好的整数5000变为整数。谢谢你的直截了当的回答。如果你也解释一下会更好。谢谢这似乎不符合原始问题的单字符字符串数组要求。ToCharArray似乎是多余的。还要注意,您得到的是System.Linq.Enumerable+whereSelectArrayInterator`2[System.Char,System.String],而不是System.String[]。
string message = "This Is A Test";
string[] result = new string[message.Length];
char[] temp = new char[message.Length];

temp = message.ToCharArray();

for (int i = 0; i < message.Length - 1; i++)
{
     result[i] = Convert.ToString(temp[i]);
}
string input = "this is a test";
string[] afterSplit = input.Split();

foreach (var word in afterSplit)
    Console.WriteLine(word);
this
is
a
test