This is to introduce the function, the Split function, where we can split the strings with a single character which can be a white space (' '), comma operator (','), or any other words.
We require the two things to split the string obviously first one the string which we want to split and the second one is the array that stores the array which we have split.
- string str="Hello, Hi Good Morning!";
- string[] strarr=str.split(' '); //split with the white space..
- string[] strarr1=str.split(','); //split with the comma operator..
The following is the foreach with which we can use to print the string array to show the string or the words or the variables.
- foreach(string str1 in strarr)
- {
- Console.WriteLine(str1);
- }
- //Output
- //Hello,
- //Hi
- //Good
- //Morning!
The following is the full source code
- using System;
- class Demo1
- {
- static void Main()
- {
- string str="Hello, Hi Good Morning!";
- string[] strarr=str.Split(' '); //split with the white space..
- string[] strarr1=str.Split(','); //split with the comma operator..
- //First String Array..
- Console.WriteLine("Frist String Array..");
- foreach(string str1 in strarr)
- {
- Console.WriteLine(str1);
- }
- Console.WriteLine("\n"+"Second String Array"+"\n");
- //Second String Array..
- foreach(string str1 in strarr1)
- {
- Console.WriteLine(str1);
- }
- }
- }
- //Output
- //First String Array..
- //Hello,
- //Hi
- //Good
- //Morning!
- //
- //Second String Array
- //Hello
- // Hi Good Morning!

Join the conversation! Your thoughts help the community grow.