Split a string

From TRCCompSci - AQA Computer Science
Revision as of 09:07, 20 May 2024 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

You can take a string and split it using a separating character:

1 Console.WriteLine("Comma separated strings");
2 // String of Vowels
3 string vowels = "A,E,I,O,U";
4 // Split vowels separated by a comma followed by space
5 string[] VowelList = vowels.Split(",");
6 foreach (string v in VowelsList)
7   Console.WriteLine(v);

You can use any separating character:

1 Console.WriteLine("Space separated strings");
2 // String of Vowels
3 string names = "Andy Erica Ian Oliver Ursula";
4 // Split vowels separated by a comma followed by space
5 string[] NamelList = names.Split(" ");
6 foreach (string n in NameList)
7   Console.WriteLine(n);