In C#, Parse string into individual characters
16,426
Solution 1
Char[] letters = word.ToCharArray();
Solution 2
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..
Solution 3
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'
Author by
C N
Updated on June 13, 2022Comments
-
C N almost 2 years
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’;