You can use this code for find single value from string
const string stringValue = "1,16,12,113";
string result = "";
const string yourValue = "1"; //Assiged the user input value
string[] arr = stringValue.Split(new char[] { ',' });//Convert string to string array
foreach (string value in arr)
{
if (value == "" + yourValue + "") // condition checked for matching your value in string array
{
result = value; //assign the matched value
break; // then go to out side of for each
}
}
string finalResult = result; //finally assigend the matching value
const string stringValue = "1,16,12,113";
string result = "";
const string yourValue = "1"; //Assiged the user input value
string[] arr = stringValue.Split(new char[] { ',' });//Convert string to string array
foreach (string value in arr)
{
if (value == "" + yourValue + "") // condition checked for matching your value in string array
{
result = value; //assign the matched value
break; // then go to out side of for each
}
}
string finalResult = result; //finally assigend the matching value
My above code is working fine .but i used foreach ,it's not a good performance ,so we can use findall
see below code and try this
const string stringValue = "1,16,12,113";
string finalValue = "";
const string yourValue = "1"; //Assiged the user input value
string[] arr = stringValue.Split(new char[] { ',' });//Convert string to string array
var results = Array.FindAll(arr, s => s.Equals(yourValue));//if your value there ,then we assigned that value in array
if (results.Length != 0)
{
finalValue = results[0];//finay we got a value
}