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

  • 질문

  • 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년 2월 19일 화요일 오후 9:14

답변

  • 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년 2월 19일 화요일 오후 11:01

모든 응답

  • 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년 2월 19일 화요일 오후 11: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년 2월 20일 수요일 오전 12: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년 2월 6일 토요일 오후 10:15
  • Yes that should work.

    -Romel Evans
    2010년 2월 7일 일요일 오후 5: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  
    • 답변으로 제안됨 Pandiyan Murugan 2019년 1월 9일 수요일 오전 4:10
    2019년 1월 8일 화요일 오전 11:53