[英]Split string into string array of single characters
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. 编辑:澄清一下,我不需要一个char数组,理想情况下,我想要一个字符串数组。 I don't really see anything wrong with the above code except for the fact that I would think there is an easier way. 除了我认为有一种更简单的方法外,我实际上没有发现上述代码有什么问题。
我相信这就是您要寻找的:
char[] characters = "this is a test".ToCharArray();
Strings in C# already have a char indexer C#中的字符串已经有一个char索引器
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. 我注意到有关char []数组的问题已更新。 If you must have a string[] array, here's how you split a string at each character in c#: 如果必须具有string []数组,请按照以下方法在c#中的每个字符处分割字符串:
string[] test = Regex.Split("this is a test", string.Empty);
foreach (string s in test)
{
Console.WriteLine(s);
}
Simple!! 简单!!
one line: 一条线:
var res = test.Select(x => new string(x, 1)).ToArray();
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);
尝试这个:
var charArray = "this is a test".ToCharArray().Select(c=>c.ToString());
Most likely you're looking for the ToCharArray() method. 您最有可能正在寻找ToCharArray()方法。 However, you will need to do slightly more work if a string[]
is required, as you noted in your post. 但是,如需要的那样,如果需要string[]
,则需要做更多的工作。
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();
}
}
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]);
}
string input = "this is a test";
string[] afterSplit = input.Split();
foreach (var word in afterSplit)
Console.WriteLine(word);
Result: 结果:
this
is
a
test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.