Introduction
There are many scenarios when you have a comma separated value and you would like to search for a particular value from the string. Normally we do a Split
operation and iterate
through the array for searching for a particular value. Here is a very easy way of searching for a value without split or iteration.
What is different in CSV string search function?
See below example where search string 33 is not present in the str
variable. isExist
will be false. if we pass some value that is in the str
variable then isExist
will be true.
string strSearch ="33";
string str = "1,3,4,56,6,62,86";
str = string.Format(",{0},", str);
bool isExist = str.Contains(string.Format(",{0},", strSearch));
I personally follow the Split and iteration operation when my data is not more than 20-30, but this method will give me good performance when I have more data to search for.
Here you can see on line 3 we have appended comma (,) on front and rear. The reason is to search for (,strSearch,) from the given input CSV string.
The code is very simple to understand so I am not going into the depths of the code.
Hope this code will help you in your projects.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.