This program is used to find the proper word from the string without using the split function.
Here is the code which is used for splitting the code with the white space, and then we can get each and every individual word in the array of the string.
- string str1="Hello , Hi Good Morning!";
- string[] str_ar1=str1.split(' ');
There is the following code which can be used to print the values from the array of the strings. Where we can get the individual word as the str from the str_ar1.
- foreach(string str in str_ar1)
- {
- console.WriteLine(str);
- }
-
-
-
-
-
-
Where we can use the if condition inside the foreach to use to match the individual word for the Matching the individual word from the string array.
- foreach(var str in str_ar1)
- {
- if(str=="Hi")
- {
- console.WriteLine("Match Found .");
- }
- else
- {
- console.WriteLine("Match Not Found");
- }
- }
But, All these things we can do with just a simple one-line code of the C# function. Which is called the Contains where we have to concate the string with the white space at starting of the character and at the end of the character for the word which is in between the string and concate the string with the white space with the ending of the word for the word starting of the string and concrete word in starting with the word for the last word of the string just like the following:
- string str="Hello,Hi Good Morning!";
- string var1="Hi"+" ";
- string var2=" "+"Hi"+" ";
- string var1=" "+"Hi";
Here, We can use the above code with the contains so we can get the exact word from the string, just like that we have done with the previous example.
- if(str.contains(var1) || str.contains(var2) || str.contains(var3))
- {
- console.WriteLine("Match Found.");
- }
- else
- {
- console.WriteLine("Match Not Found.");
- }
Here, is the full source code of the Program using the split function.
- using System;
- class Demo1
- {
- static void Main()
- {
- string str1="Hello , Hi Good Morning!";
- string[] str_ar1=str1.Split(' ');
- foreach(string str in str_ar1)
- {
- if(str=="Hi")
- Console.WriteLine("Match Found...");
- else
- Console.WriteLine("Match Not Found...");
- }
- }
- }
-
-
-
-
-
-
Here, is the Full source code of the Program using the contains and without the split code.
- using System;
- class Demo1
- {
- static void Main()
- {
- string str="Hello,Hi Good Morning!";
- string var1="Hi"+" ";
- string var2=" "+"Hi"+" ";
- string var3=" "+"Hi";
-
- if(str.Contains(var1)
- ||str.Contains(var2)
- ||str.Contains(var3))
- Console.WriteLine("Match Found...");
- else
- Console.WriteLine("Match Not Found...");
- }
- }
-
-