Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone,

I am looking for some info on using a switch statement with some strings, I don't know if it's possible or the best way as I am looking for some advice at the moment. I am going to post a snippet so everyone will get the idea.

Appreciate the help.


C#
string testing "This is a local state Florida";
string state = string.Empty;

switch(testing)
{
case "Louisiana":
if(testing.IndexOf("Louisiana") > -1)
 state = "Louisiana";
 break;

case "California":
if(testing.IndexOf("California") > -1)
 state = "California";
 break;

case "Florida";
if(testing.IndexOf("Florida") > -1)
 state = "Florida";
 break;

default:
  state = "";
}
Posted
Comments
Sergey Alexandrovich Kryukov 3-Jun-15 11:04am    
The whole switch part is a big abuse, because it makes code non-supportable. Hard-coded immediate constants are not acceptable here. You need to have a single source of each piece of data. The problem is very simple; you making out of it... who knows what.
—SA

1 solution

Your code isn't going to work as the value of "testing" does not satisfy any of your case statements

C#
string testing = "This is a local state Florida";

List<string> states = new List<string>{"Louisiana", "California", "Florida"};

string state = states.FirstOrDefault(s => testing.IndexOf(s) != -1);
 
Share this answer
 
Comments
Mario Z 3-Jun-15 8:48am    
You could replace IndexOf with Contains, it would be more appropriate for this task.
Also the string's default is null and it seems like he wants an empty string in this case so maybe you could use something like this:
string state = states.FirstOrDefault(s => testing.Contains(s)) ?? string.Empty;
F-ES Sitecore 3-Jun-15 8:50am    
I agree, I almost did that but I didn't want to deviate too far from what he originally asked :)
theadmin 3-Jun-15 13:53pm    
Mario,

Thanks again for helping me out with the other stuff. I am really chugging along after you gave me all those examples. This question pertains to the same code and I am trying to find the manufacturers of the powders. I am trying to figure out the best way to actually find the info since there will be a line with the manufacturer info contained within the string once its pulled from the html.

I used the states as an example as its pretty much close to exactly what I will be doing when I get the string. I turn to you folks as I am no coder (only in my spare time) and I am looking for guidance on how to actually perform the task at hand. It doesn't have to be a switch, I just thought of a switch since it seemed so easy. Just looking for some guidance on coding better from the pros.


Thanks guys...
Maciej Los 3-Jun-15 11:37am    
+5!
Matt T Heffron 3-Jun-15 13:17pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900