C# if string match found boolean - streamlined code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jason@cyberpine.com

    C# if string match found boolean - streamlined code?

    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.Ig noreCase |
    RegexOptions.Mu ltiline);
    if (re.Matches(col or1).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

    #2
    Re: C# if string match found boolean - streamlined code?

    how about

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

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

    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.WriteLi ne(IsColor("blu e"));
    Console.WriteLi ne(IsColor("red "));
    MyColors value;
    if (TryMatchColor( "green", out value)) {
    Console.WriteLi ne(value);
    }
    if (TryMatchColor( "turquoise" , out value)) {
    Console.WriteLi ne(value);
    }
    }
    static bool IsColor(string name) {
    MyColors color;
    return TryMatchColor(n ame, out color);
    }
    static bool TryMatchColor(s tring name, out MyColors value) {
    try {
    value = (MyColors) Enum.Parse(type of(MyColors), name, true);
    return true;
    } catch (ArgumentExcept ion) {
    value = 0; // default
    return false;
    }
    }


    Comment

    • Marc Gravell

      #3
      Re: C# if string match found boolean - streamlined code?

      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


      Comment

      • Kevin Spencer

        #4
        Re: C# if string match found boolean - streamlined code?

        public bool isit(string c1)
        {
        string[] color1 = new string[] {"blue", "green", "red"};
        return (Array.IndexOf< string>(color1, c1.ToLower()) -1);
        }

        --
        HTH,

        Kevin Spencer
        Microsoft MVP
        Chicken Salad Surgery

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


        <jason@cyberpin e.comwrote in message
        news:1156864590 .199236.32530@i 3g2000cwc.googl egroups.com...
        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.Ig noreCase |
        RegexOptions.Mu ltiline);
        if (re.Matches(col or1).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?
        >

        Comment

        • jason@cyberpine.com

          #5
          Re: C# if string match found boolean - streamlined code?


          Marc Gravell wrote:
          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!

          Comment

          • jason@cyberpine.com

            #6
            Re: C# if string match found boolean - streamlined code?


            Kevin Spencer wrote:
            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!

            Comment

            Working...