简体   繁体   中英

Finding whole number in string

I have one input number. Let's say 545. Now I have for example this string: string trying = "658;984;756;545;2000;5450" as you can see there is 545 in this string, but there is also 5450 i want to find only my input number that is 545 but in my current code I also find 5450 . My current code:

MySet.MyRole = "658;984;756;545;2000;5450";
if (MySet.MyRole is null || MySet.MyRole.IsEmpty())
{
           setupSet.MyRole1 = true;
}
else if (MySet.MyRole.Contains(Number.ToString()))
{
            setupSet.MyRole1 = true;
}

You could just Split I guess

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.

Then use Contains

Determines whether an element is in the List.

var input = "658;984;756;545;2000;5450";

var result = input.Split(';').Contains("545");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM