Answered by:
How to use string.contains() in a switch statement?

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...
Tuesday, 19 February 2008 9:14 PM
Answers
-
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)Tuesday, 19 February 2008 11:01 PM
All replies
-
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)Tuesday, 19 February 2008 11:01 PM -
I wish you had posted more code.
Code Snippetpublic
static void MethodA(){
{
{
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
Wednesday, 20 February 2008 12:58 AM -
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 SilvaSaturday, 6 February 2010 10:15 PM -
Yes that should work.
-Romel EvansSunday, 7 February 2010 5:50 PM -
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- Proposed as answer by Pandiyan Murugan Wednesday, 9 January 2019 4:10 AM
Tuesday, 8 January 2019 11:53 AM