I am wondering why there is no easy way to do this.
I just want to split a string into string array without specifying any delimiter. e. g. for my input as "Hello", I want the result as "H", "e", "l", "l", "o" i. e. an array of string.
There is a direct method given for splitting the string to character array (.ToCharArray()) and that in turn can be converted to string array, but nothing which can straight away give me string array.
Or I can't even do this:
string[] myStringArray = myString.Split(''); // Compile error
You can achieve your goal using a bit of Linq
string[] myStringArray = myString.Select(x => x.ToString())
.ToArray();
If you need to handle combining characters and surrogate pairs, you should use the StringInfo class for splitting your string:
var str = "𥀀𥀁𥀂𥀃";
var chars = new List<string>();
var tee = StringInfo.GetTextElementEnumerator(str);
while (tee.MoveNext())
chars.Add(tee.GetTextElement());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With