Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Comparing an integer to a value in a string
Message
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01560155
Message ID:
01560179
Views:
33
>>>>>>>>Hi,
>>>>>>>>
>>>>>>>>My title is not very clear so I will explain what I am looking for. I have an XML file that my ASP.NET application reads into a dataset. One of the columns of the dataset has a string of the following format (examples):
>>>>>>>>"1,3,29"
>>>>>>>>"2"
>>>>>>>>"4,5,1"
>>>>>>>>That is the string contains number(s) separated by commas (btw, if the answer to my question involves changing the separator from comma to something different, I can do it).
>>>>>>>>Then I need to compare if a certain number (integer) is in the string. For example, say a variable iNumber is within the string. E.g. in pseudo code:
>>>>>>>>
>>>>>>>>iNumber = 3
>>>>>>>>cString = "2,4,12"
>>>>>>>>If (iNumber = Withint( cSring))
>>>>>>>>{ 
>>>>>>>>    // process
>>>>>>>>}
>>>>>>>>
>>>>>>>>
>>>>>>>>Any suggestions on how to compare the number (iNumber) to the content of the string? TIA.
int iNumber = 2;
>>>>>>>string cstring = "2,4,12";
>>>>>>>
>>>>>>>bool isWithin = cstring.Split(',').FirstOrDefault(x => Convert.ToInt32(x) == iNumber) != null;
>>>>>>
>>>>>>Thank you. I wonder though if the syntax "x =>" is something new, after .NET 2.0; since I have never seen it (and I am still working in .NET 2.0). But the key, as others suggested, is learning to "Split" (pun intended).
>>>>>
>>>>>It's Linq. Think it came in .NET 3.5 ?
>>>>>Even simpler, BTW :
bool isWithin = cstring.Split(',').Contains(iNumber.ToString());
>>>>
>>>>Again, thank you very much. As I just mentioned to Gregory, I can't seem to see the Contains in my list of methods (in Intellisense).
>>>
>>>As Gregory suggests:
bool isWithin = Array.IndexOf(cstring.Split(','), iNumber.ToString()) > 0;
but why don't you move on to a later VS ?
>>
>>Fear of breaking the code <g>.
>
>VERY unlikely. I've moved through every VS version and cannot recall a problem.....
>
>>Thank you for the answer

I do plan to take a few days and make the change. I don't want to update the current project to VS 2012 but rather copy it and have a "new" project" in VS 2012. This way I can always fall back on the existing VS 2005 project.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform