C# 将字符串拆分为单个字符的字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9846948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split string into string array of single characters
提问by mowwwalker
I want to something as simple as turning "this is a test"into
我想一些简单的转"this is a test"成
new string[] {"t","h","i","s"," ","i","s"," ","a"," ","t","e","s","t"}
Would I really have to do something like
我真的必须做类似的事情吗
test = "this is a test".Select(x => x.ToString()).ToArray();
edit: To clarify, I don't want a char array, ideally I want an array of string. I don't really see anything wrong with the above code except for the fact that I would think there is an easier way.
编辑:澄清一下,我不想要一个字符数组,理想情况下我想要一个字符串数组。除了我认为有一种更简单的方法之外,我并没有真正看到上面的代码有什么问题。
采纳答案by Brandon Moretz
I believe this is what you're looking for:
我相信这就是你要找的:
char[] characters = "this is a test".ToCharArray();
回答by David
You can just use String.ToCharArray()and then treat each char as a string in your code.
您可以只使用String.ToCharArray()然后将每个字符视为代码中的字符串。
Here's an example:
下面是一个例子:
foreach (char c in s.ToCharArray())
Debug.Log("one character ... " +c);
回答by Chris Gessler
Strings in C# already have a char indexer
C# 中的字符串已经有一个字符索引器
string test = "this is a test";
Console.WriteLine(test[0]);
And...
和...
if(test[0] == 't')
Console.WriteLine("The first letter is 't'");
This works too...
这也有效...
Console.WriteLine("this is a test"[0]);
And this...
和这个...
foreach (char c in "this is a test")
Console.WriteLine(c);
EDIT:
编辑:
I noticed the question was updated with regards to char[] arrays. If you must have a string[] array, here's how you split a string at each character in c#:
我注意到有关 char[] 数组的问题已更新。如果必须有一个 string[] 数组,以下是在 c# 中在每个字符处拆分字符串的方法:
string[] test = Regex.Split("this is a test", string.Empty);
foreach (string s in test)
{
Console.WriteLine(s);
}
回答by David Peden
Try this:
尝试这个:
var charArray = "this is a test".ToCharArray().Select(c=>c.ToString());
回答by ahawker
Most likely you're looking for the ToCharArray()method. However, you will need to do slightly more work if a string[]is required, as you noted in your post.
您很可能正在寻找ToCharArray()方法。但是,string[]如您在帖子中所述,如果需要a,您将需要做更多的工作。
string str = "this is a test.";
char[] charArray = str.ToCharArray();
string[] strArray = str.Select(x => x.ToString()).ToArray();
Edit: If you're worried about the conciseness of the conversion, I suggest you make it into an extension method.
编辑:如果你担心转换的简洁性,我建议你把它变成一个扩展方法。
public static class StringExtensions
{
public static string[] ToStringArray(this string s)
{
if (string.IsNullOrEmpty(s))
return null;
return s.Select(x => x.ToString()).ToArray();
}
}
回答by cheziHoyzer
Simple!!
one line:
简单的!!
一条线:
var res = test.Select(x => new string(x, 1)).ToArray();
回答by William
Convert the message to a character array, then use a for loop to change it to a string
将消息转换为字符数组,然后使用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]);
}
回答by Last2Night
string input = "this is a test";
string[] afterSplit = input.Split();
foreach (var word in afterSplit)
Console.WriteLine(word);
Result:
结果:
this
is
a
test

