Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into string array of single characters

Tags:

string

c#

split

I want to something as simple as turning "this is a test" into

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.

like image 933
mowwwalker Avatar asked Mar 23 '12 21:03

mowwwalker


People also ask

How do I convert a string to an array of one string?

Using StringBufferCreate an empty String Buffer object. Traverse through the elements of the String array using loop. In the loop, append each element of the array to the StringBuffer object using the append() method. Finally convert the StringBuffer object to string using the toString() method.

What method allows us to break a string into an array of individual characters?

Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.


1 Answers

I believe this is what you're looking for:

char[] characters = "this is a test".ToCharArray(); 
like image 157
Brandon Moretz Avatar answered Sep 19 '22 17:09

Brandon Moretz