2

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.

1

3 Answers 3

7

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this, this will do the job :)
1

You can use Linq to quickly transform it:

strToBeSplitted.Select(c => c.ToString()).ToArray();

1 Comment

ToCharArray is not necessary, you can directly apply Select, See my answer below
0

For completeness, RegEx approach to split between characters:

string[] charReturn = Regex.Split("HelloWorld", "(?!^)(?<!$)");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.