Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string contains a number c#

I know that there is lots of questions like this out there. But I really couldn't find anything that solved my problem.

I want to check if the string contains the 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 is 3 and not 30. So it is the contains() that is the problem.

How do I make it search for 3 only?

like image 496
n.Stenvang Avatar asked Nov 28 '22 23:11

n.Stenvang


2 Answers

You could use String.Split + Contains:

bool contains3 = input.Split('.').Contains("3");
like image 116
Tim Schmelter Avatar answered Dec 05 '22 04:12

Tim Schmelter


bool anyThree = input.Split('.').Any(str => str == "3");
like image 31
Amir Popovich Avatar answered Dec 05 '22 04:12

Amir Popovich