Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi
I have a string which has all the system names in a comma separated form.
Example:

C#
string systemNames="system1,system2,system3,system4";


From this string I need to find a system name whether the system name exists or not.

For example
I give "system1 " I should get as "TRUE"<br />
I give "system10" I should get as "False"<br />


Kindly Help me to solve this , The code should have any Loop.
Posted
Updated 5-Apr-11 1:02am
v2

C#
private bool SystemExists(string systemName)
{
    text = text.Trim().ToLower();
    string[] parts = systemNames.Split(',');
    int count = (from part in parts
                 where part.Trim().ToLower() == systemName
                 select part).Count();
    return (count > 0);
}


If you want it to be case-sensitive, just remove the calls to string.ToLower().

EDIT ===========

Why was this voted a 1? It's correct, and fulfills the stated needs of the OP.
 
Share this answer
 
v5
If the pattern of the string remains the same, you cna use this:

string inputValue = originalInput + ",";
bool result = systemNames.ToUpper().Contains(inputValue.ToUpper());


I am not sure if there is any IgnoreCase overload for the Contains method. If there is, you can use that. You can also do trimming wherever needed.
 
Share this answer
 
Comments
arindamrudra 5-Apr-11 7:25am    
Danish, your solution will not work for "system1" and "system10" because "system1" contains in both cases. So, Mahen's answer is more appropriate and Tarun's answer is the best. Please update it.
dan!sh 5-Apr-11 7:33am    
You probably missed the line where I have added the comma after the input.
arindamrudra 5-Apr-11 7:42am    
Yes, I missed that one. Sorry for my comment. Your answer is correct.
dan!sh 5-Apr-11 7:46am    
No problems. :)
lukeer 5-Apr-11 8:36am    
But then, what if you should happen to search for the last system in systemNames? That one is not followed by ','.
Use split function like this :

C#
string systemNames = "system1,system2,system3,system4";
String[] arr = systemNames.Split(',');

String mySystemName="System1";

foreach(String system in arr)
{
    if(system.Equals(mySystemName))
    {
        //You have found the system
    }
}


Hope it helped! :)
 
Share this answer
 
v2
Hi,
this should be enough,
systemNames.Split(new[] { ',' }).Contains(inputValue);

or if you have a harsh code reviewer :)
systemNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select((item)=>item.ToLower(CultureInfo.InvariantCulture).Trim()).Contains(inputValue.ToLower(CultureInfo.InvariantCulture).Trim());


Regards
 
Share this answer
 
v3
Comments
Manfred Rudolf Bihy 5-Apr-11 8:34am    
Good one! 5+
Tarun.K.S 5-Apr-11 9:01am    
I don't think so. System1 and System11 will be same in this case. You should use "Equals" instead of "Contains".
Ciumac Sergiu 5-Apr-11 9:07am    
Wrong. Array.Contains() is not the same as string.Contains().
Tarun.K.S 5-Apr-11 9:17am    
Oops you are right. Sorry buddy it was my mistake.
#realJSOP 5-Apr-11 9:17am    
You have to trim and then normalize the case before performing the comparison. You're making to many assumptions.
C#
char[] separator = new char[] { ',' };
                string[] strProductId = YourString.Split(separator);
                for (int i = 0; i < strProductId.Length - 1; i++)
                {
                   string  stringId = Convert.Tostring(strProductId[i]);
                    if( string stringId  =="Your string")
                        {
                          flag =  true
                         }
                    else
                       {
                         flag = false
                        }
                     }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900