- Home /
Using the StartsWith function in a Switch Case Statement
I have a quick question that I cant find an answer to:
How come I can say (importing the system namespace of course):
if(someString.StartsWith("bla")){
do something...
}
but I can't say:
switch(someString.StartsWith){
case "bla":
do this...
case "idk":
do that...
}
Thanks.
Answer by syclamoth · Oct 09, 2011 at 10:27 PM
someString.StartsWith("whatever") returns a bool, not a string! You can't use it like that. If you're trying to get the first 3 characters of the string, you would use
switch(someString.SubString(0, 3))
{
case "bla":
do some things...
case "idk":
do something else...
}
This is compatible with using a switch in this way, because it returns a string which can then be compared in your cases!
Your answer
Follow this Question
Related Questions
What is wrong with my c# switch statement? 1 Answer
String Scanning Switch Statement 1 Answer
want to convert too many if statements into 1 switch case statement. error help. 2 Answers
How to check if an enum’s case has changed 3 Answers
What is a more efficient way to write this Switch Statement? 3 Answers