Tuesday 25 June 2013

How to find single value from string in c#

         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


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
    }

3 comments:

If any doubt?then please comment in my post

How to reduce angular CLI build time

 Index: ----- I am using angular cli - 7 and I am going to tell how to reduce the build time as per my knowledge. Problem: -------- Now the ...