[c#] Split string into string array of single characters

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.

This question is related to c# string split

The answer is


Try this:

var charArray = "this is a test".ToCharArray().Select(c=>c.ToString());

string input = "this is a test";
string[] afterSplit = input.Split();

foreach (var word in afterSplit)
    Console.WriteLine(word);

Result:

this
is
a
test

Most likely you're looking for the ToCharArray() method. However, you will need to do slightly more work if a string[] is required, as you noted in your post.

    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();
    }
} 

Simple!!
one line:

 var res = test.Select(x => new string(x, 1)).ToArray();

Convert the message to a character array, then use a for loop to change it to a string

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]);
}

You can just use String.ToCharArray() and then treat each char as a string in your code.

Here's an example:

    foreach (char c in s.ToCharArray())
        Debug.Log("one character ... " +c);

Strings in C# already have a char indexer

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. If you must have a string[] array, here's how you split a string at each character in c#:

string[] test = Regex.Split("this is a test", string.Empty);

foreach (string s in test)
{
  Console.WriteLine(s);
}

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to split

Parameter "stratify" from method "train_test_split" (scikit Learn) Pandas split DataFrame by column value How to split large text file in windows? Attribute Error: 'list' object has no attribute 'split' Split function in oracle to comma separated values with automatic sequence How would I get everything before a : in a string Python Split String by delimiter position using oracle SQL JavaScript split String with white space Split a String into an array in Swift? Split pandas dataframe in two if it has more than 10 rows