I have a string:
string strToBeSplitted = "HelloWorld";
And I am planning to split my string to an array of string. Usually we do it with char:
char[] charReturn = strToBeSplitted.ToCharArray();
But what I am planning to do is return it with an array of string like this one:
string[] strReturn = strToBeSplitted ???
//Which contains strReturn[0] = "H"; and so on...
I want to return an array of string but I cannot figure out how to do this unless I do this manually by converting it to char then to a new string like StringBuilder.
You can use .Select
which will iterate through each characters in the given string, and .ToString()
will help you to convert a character to a string, and finally .ToArray()
can help you to store the IEnumerable<string>
into a string array. Hope that this is what you are looking for:
string strToBeSplitted = "HelloWorld";
string[] strArray = strToBeSplitted.Select(x => x.ToString()).ToArray();
您可以使用Linq快速转换它:
strToBeSplitted.Select(c => c.ToString()).ToArray();
为了完整性, RegEx
方法分割字符:
string[] charReturn = Regex.Split("HelloWorld", "(?!^)(?<!$)");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.