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(' ');
- string[] strarr1=str.split(',');
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);
- }
-
-
-
-
-
The following is the full source code
- using System;
- class Demo1
- {
- static void Main()
- {
- string str="Hello, Hi Good Morning!";
- string[] strarr=str.Split(' ');
- string[] strarr1=str.Split(',');
-
- Console.WriteLine("Frist String Array..");
- foreach(string str1 in strarr)
- {
- Console.WriteLine(str1);
- }
- Console.WriteLine("\n"+"Second String Array"+"\n");
-
- foreach(string str1 in strarr1)
- {
- Console.WriteLine(str1);
- }
- }
- }
-
-
-
-
-
-
-
-
-
-