Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

C# if string match found boolean - streamlined code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jason@cyberpine.com - 29 Aug 2006 16:16 GMT
Though this code appears to work, I suspect it could be streamline.
Total Noob Here.

       public bool isit(string c1)
       {
           string color1 = "blue green red";
           Regex re = new Regex("@"+c1, RegexOptions.IgnoreCase |
RegexOptions.Multiline);
          if (re.Matches(color1).Count > 0)
           {
               return (true);
           }
           else
           {
               return (false);
           }

==
I found a sample on the net that does this

if (re.color1 != 0 )

But I get an error about Regex not having  a definition for color1

==
Also, what if color1 was an enum, how would test the search string can
be found in any of the items?
Marc Gravell - 29 Aug 2006 16:31 GMT
how about

switch(c1) {
 case "blue":
 case "green":
 case "red":
   return true;
 default:
   return false;
}

More generally, you could use
return re.IsMatch(color1);

If it was an enum, then the Enum helper class can do a lot (i.e. list
available items, parse items, etc); e.g.

       enum MyColors { Blue, Red, Green };
       static void Main(string[] args) {
           Console.WriteLine(IsColor("blue"));
           Console.WriteLine(IsColor("red"));
           MyColors value;
           if (TryMatchColor("green", out value)) {
               Console.WriteLine(value);
           }
           if (TryMatchColor("turquoise", out value)) {
               Console.WriteLine(value);
           }
       }
       static bool IsColor(string name) {
           MyColors color;
           return TryMatchColor(name, out color);
       }
       static bool TryMatchColor(string name, out MyColors value) {
           try {
               value = (MyColors) Enum.Parse(typeof(MyColors), name, true);
               return true;
           } catch (ArgumentException) {
               value = 0; // default
               return false;
           }
       }
Marc Gravell - 29 Aug 2006 16:35 GMT
OK - reading again I may have missed your intention... forgetting about
solving it via Regex (or any other approach) - what exactly are you trying
to do? i.e. input {x}, output {y}...?

Marc
jason@cyberpine.com - 30 Aug 2006 02:53 GMT
> OK - reading again I may have missed your intention... forgetting about
> solving it via Regex (or any other approach) - what exactly are you trying
> to do? i.e. input {x}, output {y}...?
>
> Marc

The post with the array  is going to work.. I wanted to use Regex
because some of the entries will be sentences and I might need to
search for patterns. I know I just asked for a boolean, but Ideally I
wanted to return an array of every entry where the pattern was found.

Thank you!
Kevin Spencer - 29 Aug 2006 16:53 GMT
public bool isit(string c1)
{
    string[] color1 = new string[] {"blue", "green", "red"};
   return (Array.IndexOf<string>(color1, c1.ToLower()) > -1);
}

Signature

HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.

> Though this code appears to work, I suspect it could be streamline.
> Total Noob Here.
[quoted text clipped - 23 lines]
> Also, what if color1 was an enum, how would test the search string can
> be found in any of the items?
jason@cyberpine.com - 30 Aug 2006 02:56 GMT
> public bool isit(string c1)
> {
>      string[] color1 = new string[] {"blue", "green", "red"};
>     return (Array.IndexOf<string>(color1, c1.ToLower()) > -1);
> }

Thank you.. this is a great help.

if you have it handy.. what if the enteries were very long sentences
and It was a pattern I was searching for (I simplified my original
question).  And what if I wanted to return an array of lines that had
the match. I know I might be pushing it and if so I'll go dig around -
I have a very similar VB code I'm trying to migrate over.

Thanks again - really appreciate it!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.