Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Comparing an integer to a value in a string
Message
From
20/12/2012 11:25:34
 
 
To
20/12/2012 11:21:46
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01560155
Message ID:
01560303
Views:
23
>>>>>>>>>>>>>>>>>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;
>>>>>>>>>>>
>>>>>>>>>>>This is not a complaint but simply to make others who may read this thread not to be bitten by the fact .NET array are zero based. So when the IndexOf() returns 0, it means that the element exist in the array. I think the code above should be > -1. Am I correct?
>>>>>>>>>>
>>>>>>>>>>Yes (and it should be a complaint ! :-}) ; Gregory had it right ( >=0) but -1 is probably better....
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>haha - why is -1 better ?
>>>>>>>>
>>>>>>>>Looks prettier ? :-} . I'd guess they both compile to the same thing tho..
>>>>>>>>
>>>>>>>>>I think the best is
>>>>>>>>>
>>>>>>>>> >= GetLowerBound(0)
>>>>>>>>
>>>>>>>>I like:
bool isWithin = (Array.Find(cstring.Split(','), delegate(string s) { return s == iNumber.ToString(); })) !=null;
:-}
>>>>>>>
>>>>>>>
>>>>>>>Ah, a predicate
>>>>>>>
>>>>>>>Not that it matters in this small example, but I'm inclined to take iNumber.ToString() out of the loop
>>>>>>>
>>>>>>>			string s = "2,3,25";
>>>>>>>
>>>>>>>			int i = 5;
>>>>>>>			string y= i.ToString();
>>>>>>>			bool isIn = Array.Find(s.Split(','), delegate(string x){ return x ==  y;}) != null;
>>>>>>>
>>>>>>
>>>>>>You two are show-offs <g>
>>>>>
>>>>>
>>>>>No, we browse this book from time to time http://www.amazon.com/5-0-Nutshell-The-Definitive-Reference/dp/1449320104/ref=sr_1_1?ie=UTF8&qid=1356011773&sr=8-1&keywords=c+in+a+nutshell+5.0
>>>>
>>>>I've only got the C# 4.0 copy :-{
>>>>But my two favourites are the 'Effectve c#' and 'More Effective C#' books from Wagner.....
>>>
>>>
>>>Think I've recommended those
>>
>>You could well be right. In which case thx. Any other reccommedations ?
>
>
>Found it Re: View design Thread #1496981 Message #1497073

Yup. Bought them just after......
Previous
Reply
Map
View

Click here to load this message in the networking platform