locked
How to use string.contains() in a switch statement? RRS feed

  • Question

  • How would i do this:

     

    String text = "here is a string with some text";

     

     

    switch (text.Contains())
      {
    case "text" :

    echo "text" was found;
     break;

     

    case "image" :

    echo "image was found in the string";
    break;

     

    case "something else":

    echo "something else was found";

    break;
      }

     

    I assume you understand what i try to achieve here - but obviously switch (text.Contains()) won't work, because it only returns true or false...

    mardi 19 février 2008 21:14

Réponses

  • The simple answer is Yes, the switch won't work like that. Just use if() blocks.

    The more complex option if you're looking for specific keywords (i.e. like writing an SQL parser etc.) you can opt to use regular expressions to search for labelled matches then determine if any matches were found for each of your keywords. This will be less read-able but may likely net faster performance in cases where the search text and number of search conditions are larger.


    Start with if() blocks then if the performance is an issue read up on regular expressions. (regex)
    mardi 19 février 2008 23:01

Toutes les réponses

  • The simple answer is Yes, the switch won't work like that. Just use if() blocks.

    The more complex option if you're looking for specific keywords (i.e. like writing an SQL parser etc.) you can opt to use regular expressions to search for labelled matches then determine if any matches were found for each of your keywords. This will be less read-able but may likely net faster performance in cases where the search text and number of search conditions are larger.


    Start with if() blocks then if the performance is an issue read up on regular expressions. (regex)
    mardi 19 février 2008 23:01
  • I wish you had posted more code.

     

    Code Snippet

    public static void MethodA()

    {

    string text = "here is a string with some text";

    string[] testString = new string[] { "here", "is", "a", "text", "value" };

    foreach (string s in testString)

    {

    switch (text.Contains(s))

    {

    case true:

    MessageBox.Show(s + "was found");

    break;

    default:

    MessageBox.Show(s + "was not found");

    break;

    }

    }

    }

     

     

    Hope this helps.  I have a personal thing against using if.  I learned to program without them.  I've had to debug code that contained convoluted and nested statementes, which are hard to debug.   

     

    There's just too many variations on an if statement to make it easy to maintain.  Granted, in most cases switch or if will compile to nearly identical IL code.  But which is easier for a human being to read.  I challenge anyone to come up with a set of if statements that cannot be re-written using switch.  You cannot, because anything can be re-coded to use switch.

     

    Rudedog

    mercredi 20 février 2008 00:58
  • What if I want to do this validation, but with different actions?


     string auxStr = subkey.ToLower();
                    switch (auxStr)
                    {
                        case "data source=":  
                                                 system.engine = subkey.Substring(subkey.IndexOf("=") + 1); 
                                                 break;
                        case "initial catalog=": 
                                                 system.database = subkey.Substring(subkey.IndexOf("=") + 1); 
                                                 break;
                        case "user id=":         
                                                 system.user = subkey.Substring(subkey.IndexOf("=") + 1); 
                                                 break;
                        case "password=":        
                                                 system.pwd = subkey.Substring(subkey.IndexOf("=") + 1); 
                                                 break;
                        default:                 
                                                 break;
                    }
    Is that possible?

    Thanks in advance,

    Nuno
    Nuno Silva
    samedi 6 février 2010 22:15
  • Yes that should work.

    -Romel Evans
    dimanche 7 février 2010 17:50
  • I think I should update this thread because it was one of my first results in google. It looks like this is possible as of C# 7.0.

    https://efficientuser.com/2017/03/15/c-7-pattern-matching-in-switch  
    mardi 8 janvier 2019 11:53