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

  • Kérdés

  • 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...

    2008. február 19., kedd 21:14

Válaszok

  • 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)
    2008. február 19., kedd 23:01

Az összes válasz

  • 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)
    2008. február 19., kedd 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

    2008. február 20., szerda 0: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
    2010. február 6., szombat 22:15
  • Yes that should work.

    -Romel Evans
    2010. február 7., vasárnap 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  
    2019. január 8., kedd 11:53