Groups | Blog | Home
all groups > c# > april 2008 >

c# : String Comparison


Qu
4/15/2008 8:20:10 PM
Hi everyone.

I'm trying to make an application in which people can type in their
own selection filter for an external application. For this, I need to
be able to compare a string to a value in much the same way a search
box does: "*" for a wildcard, AND and OR as their respective
comparisons, greater than> and less than<, etc...

Here's an example. The user could type this into the textbox:
">100 And <180 Or 240"
Which would be compared with a number and return true if the number
was between 100 and 180, or was 240.

Another example:
"*BRACE* Or SINGLE_BEAM"
Which would be compared with a string and return true if the string
contained the substring BRACE or exactly matched SINGLE_BEAM.

Is there an easy way to do this, or am I going to have to go through
the string character by character to extract the information? [crosses
fingers]

Thanks,
Qu ~ Quasar Tri-Delta ~
4/15/2008 9:37:37 PM
On Apr 16, 1:35=A0pm, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
[quoted text, click to view]

Thanks for the reference, I haven't used regular expressions or the
regex class before. I'll have to study them a bit, but they seem to be
fairly close to what I'm looking for. I just need to work out how
Cor Ligthert[MVP]
4/16/2008 5:35:21 AM
Qu,

In my idea is mostly using the split the first way I try, probably is your
problem however a real Regex problem.

http://msdn2.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

Cor

"Qu" <quasar@beagle.com.au> schreef in bericht
news:ebdb36fc-16cb-4dca-a35f-b1bff580fad9@w5g2000prd.googlegroups.com...
[quoted text, click to view]
Ignacio Machin ( .NET/ C# MVP )
4/16/2008 8:18:03 AM
[quoted text, click to view]

Hi,

No, there is no easy way of doing that. You need to parse the string
(not an easy task per se) and generate a Regex struct to later use to
Qu ~ Quasar Tri-Delta ~
4/16/2008 2:54:26 PM
On Apr 17, 1:18=A0am, "Ignacio Machin ( .NET/ C# MVP )"
[quoted text, click to view]

Looking into the help files on regular expressions I began to suspect
as much. Oh well... at least its a function I can write once and use
Tigger
4/20/2008 11:56:02 PM
[quoted text, click to view]

Maybe linq could be a solution. I'm still on .Net 2.0 so I'm not familiar
with its limitations. (or the select method in a dataset has some
abilities).

If this is to gather data in a database, then maybe your database supports
this sort of expression.

Otherwise your asking quite a lot as the system that parses these
expressions has to understand boolean logic across numbers and strings plus
partial string matching. And it has to cope with user error!

Generally this means you need to implement a parser, and this is quite
involved

I found the following a good tool to develop one in .Net...
http://www.devincook.com/goldparser/
Far better than going through, character by character!

It sounds like you interface is not GUI based. If it was then one way would
be to make the GUI provide an editor that controls the filter logic a user
can enter. In this way you can enforce the syntax making writing the filter
code easier. In my GUIs i tend to let the user do from/to for number fields
and partial string matching for text fields. I combine them together (AND)
and this covers most peoples needs without getting complex.

--
Tony
http://www.mccreath.org.uk




AddThis Social Bookmark Button