Click here to Skip to main content
11,930,449 members (56,861 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked

Searching method for CSV strings: A different way

, 5 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A new way of searching from a CSV string.


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 article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralWon't pick up items at the start or end of a row Pin
John Brett5-Jul-12 7:14
memberJohn Brett5-Jul-12 7:14 
GeneralRe: Won't pick up items at the start or end of a row Pin
Andreas Kroll6-Jul-12 10:48
memberAndreas Kroll6-Jul-12 10:48 
GeneralRe: Won't pick up items at the start or end of a row Pin
@amitgajjar8-Jul-12 19:05
member@amitgajjar8-Jul-12 19:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 5 Jul 2012
Article Copyright 2012 by @AmitGajjar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid