Difference between revisions of "Split a string"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "You can take a string and split it using a separating character: <syntaxhighlight lang=csharp line> Console.WriteLine("Comma separated strings"); // String of Vowels string v...")
 
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
 
foreach (string v in VowelsList)
 
foreach (string v in VowelsList)
 
   Console.WriteLine(v);
 
   Console.WriteLine(v);
 +
</syntaxhighlight>
 +
 +
You can use any separating character:
 +
 +
<syntaxhighlight lang=csharp line>
 +
Console.WriteLine("Space separated strings");
 +
// String of Vowels
 +
string names = "Andy Erica Ian Oliver Ursula";
 +
// Split vowels separated by a comma followed by space
 +
string[] NamelList = names.Split(" ");
 +
foreach (string n in NameList)
 +
  Console.WriteLine(n);
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 09:07, 20 May 2024

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