Check The Word Is There In The String Or Not Without The Split()

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.
  1. string str1="Hello , Hi Good Morning!";      
  2. 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.
  1. foreach(string str in str_ar1)    
  2. {    
  3.        console.WriteLine(str);    
  4. }  
  5. //Output  
  6. //Hello  
  7. //,  
  8. //Hi  
  9. //Good  
  10. //Morning!   
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.
  1. foreach(var str in str_ar1)  
  2. {  
  3.    if(str=="Hi")  
  4.    {  
  5.       console.WriteLine("Match Found .");  
  6.    }  
  7.    else  
  8.    {  
  9.       console.WriteLine("Match Not Found");  
  10.    }  
  11. }  
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:
  1. string str="Hello,Hi Good Morning!";
  2. string var1="Hi"+" "//For the First Word of the string.  
  3. string var2=" "+"Hi"+" ";//For the Word in between the string neither first nor last.  
  4. string var1=" "+"Hi";  //For the Last Word of the string.  
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.
  1. if(str.contains(var1) || str.contains(var2) || str.contains(var3))  
  2. {  
  3.    console.WriteLine("Match Found.");  
  4. }  
  5. else  
  6. {  
  7.    console.WriteLine("Match Not Found.");  
  8. }   
Here, is the full source code of the Program using the split function.
  1. using System;  
  2. class Demo1  
  3. {  
  4.     static void Main()   
  5.     {  
  6.         string str1="Hello , Hi Good Morning!"//string to find the word  
  7.         string[] str_ar1=str1.Split(' ');  
  8.         foreach(string str in str_ar1)  
  9.         {  
  10.             if(str=="Hi")  
  11.                 Console.WriteLine("Match Found...");  
  12.             else  
  13.                 Console.WriteLine("Match Not Found...");  
  14.         }  
  15.     }  
  16. }  
  17. //Output  
  18. //Match Not Found...  
  19. //Match Not Found...  
  20. //Match Found...  
  21. //Match Not Found...  
  22. //Match Not Found...   
Here, is the Full source code of the Program using the contains and without the split code. 
  1. using System;  
  2. class Demo1  
  3. {  
  4.     static void Main()   
  5.     {  
  6.         string str="Hello,Hi Good Morning!";  
  7.         string var1="Hi"+" "//For the First Word of the string.    
  8.         string var2=" "+"Hi"+" ";//For the Word in between the string neither first nor last.    
  9.         string var3=" "+"Hi";  //For the Last Word of the string.    
  10.           
  11.         if(str.Contains(var1)//Compare with Starting word..  
  12.         ||str.Contains(var2)//Compare with center words of the strings..  
  13.         ||str.Contains(var3))//Compare with the last word..  
  14.             Console.WriteLine("Match Found...");  
  15.         else  
  16.             Console.WriteLine("Match Not Found...");  
  17.     }  
  18. }  
  19. //Output  
  20. //Match Found