82

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.

2
  • Do you really want each character as a string? Commented Mar 23, 2012 at 21:55
  • 8
    What is wrong with your code var test = "this is a test".Select(x => x.ToString()).ToArray(); ? Commented Mar 23, 2012 at 21:58

8 Answers 8

166

I believe this is what you're looking for:

char[] characters = "this is a test".ToCharArray();
Sign up to request clarification or add additional context in comments.

9 Comments

Although this isn't what I asked for, I think I'm starting to realize that there really isn't a reason for what I asked.
@Walkerneo What do you really want? a -1?
@Walkerneo That's why I answered with this. I couldn't think of a reason you'd want an array of single character strings. No worries though.
@BrandonMoretz I just voted up so that your rep changes from a very nice round number (5000). Thanks for the sharp to the point answer.
@BrandonMoretz The answer you linked to doesn't contradict what I said? The iterator you'd use to split a string into graphemes, TextElementEnumerator, produces strings rather than chars precisely for this reason (and also because of combining marks).
|
43

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

6 Comments

+1 for hitting an obvious point that I missed when posting my answer. Duh. Why didn't I think of that?
They have a char indexer, and they implement IEnumerable<char>, but they're not actually a char[] or an IList<char>.
true. they are of type string, but the point is that the developer does not need to use ToCharArray() or any type of conversion to string[] to access the individual characters.
Mr . @CSharper Me walking the same road now. And It worked for me.
interesting that Regex.Split treats the string as if there was an empty string before the first and after the last character
|
9

Simple!!
one line:

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

Comments

6

Try this:

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

1 Comment

The ToCharArray() seems to be redundant. Also note that you get System.Linq.Enumerable+WhereSelectArrayIterator`2[System.Char,System.String] instead of System.String[].
5

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

Comments

4

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

Comments

1

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

Comments

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

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

Result:

this
is
a
test

3 Comments

It would be better if you explain it a bit as well. Thanks!
This doesn't seem to meet the "string array of single characters" requirement of the original question.
This splits it into words, not single characters

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.