Check if string contains C # number

I know that there are many such questions. But I really could not find anything that could solve my problem.

I want to check if a string contains a specific input number. See the following example:

public Boolean checkString()
{
    string input = "2.5.8.12.30";
    string intToFind = "3";

    if (input.contains(intToFind))
    {
        return true;
    }
    else
    {
        return false;
    }
}

This returns true, but I want it to return false, since the intToFind string has a value of 3, not 30. Thus, the contains () problem is the problem.

How do I search for only 3?

+4
source share
5 answers

You can use String.Split+ Contains:

bool contains3 = input.Split('.').Contains("3");
+13
source
bool anyThree = input.Split('.').Any(str => str == "3");
+7
source

, String.Split('.') . Array.contains, ,

bool contained = input.Split('.').Contains("3");
+2
string[] words = input.Split('.');
if (words.contains("3")){do something...}
0

You can also use Regex, which can overdo it a bit.

string str = "2.5.8.12.30";
string strToFind = "3";
bool contains = Regex.Match(str, string.Format(@"\W{0}\W",strToFind)).Success;
0
source

All Articles