-3

How do I find out if one of my strings occurs in the other in C#?

For example, there are 2 strings: string 1= "The red umbrella"; string 2= "red"; How would I find out if string 2 appears in string 1 or not?

  • [`string.Contains`](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netframework-4.7.2) – juharr Mar 22 '19 at 02:05

2 Answers2

1

you can use String Contains I guess. pretty sure there is similar question have been asked before How can I check if a string exists in another string

example :

 if (stringValue.Contains(anotherStringValue))
{  
// Do Something // 
 }
Nice Guy
  • 41
  • 12
-2

You can do it like this:

public static bool CheckString(string str1, string str2)
{   
    if (str1.Contains(str2))
    {
        return true;
    }
    return false;
}

Call the function:

CheckString("The red umbrella", "red") => true
Yanga
  • 2,885
  • 1
  • 29
  • 32