3

In C#, how do you parse a string into individual characters?
Given:
word = “Wonderful”;

Desired Result:
letter[0] = ‘W’;
letter[1] = ‘o’;
letter[2] = ‘n’;
letter[3] = ‘d’;
letter[4] = ‘e’;
letter[5] = ‘r’;
letter[6] = ‘f’;
letter[7] = ‘u’;
letter[8] = ‘l’;

3 Answers 3

18
Char[] letters = word.ToCharArray(); 
Sign up to request clarification or add additional context in comments.

4 Comments

This works, but I am using @chemicalNova as the accepted answer because there are less steps. I do appreciate your answer! I added a point to your useful points.
@James it's a QA site so all knowledge is valuable, even the simplistic things
@CN you stated you wanted a character array, his you have to set each one invidually, not sure how it's easier
@msarchet Going through my program. I found out that it was optimal for me to use your character array. I made your answer the accepted answer.
6

Strings actually have an indexer method for that ...

string word = "Wonderful"; char letter1 = word[0]; // W char letter2 = word[1]; // o char letter3 = word[2]; // n 

etc..

1 Comment

I used your answer as the accepted answer, because it has the least amount of steps.
4

You don't have to do anything at all. You can just access the characters by index from the string.

Given:

string word = "Wonderful"; 

You have:

word[0] = 'W' word[1] = 'o' word[2] = 'n' word[3] = 'd' word[4] = 'e' word[5] = 'r' word[6] = 'f' word[7] = 'u' word[8] = 'l' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.