使用IndexOf切换的最佳方法 [英] Best way to use switch with IndexOf

查看:90
本文介绍了使用IndexOf切换的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在寻找关于使用带有一些字符串的switch语句的一些信息,我不知道是否可能或最好的方式我正在寻找一些建议。我将发布一个片段,以便每个人都能得到这个想法。



感谢帮助。





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.


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 = "";
}

推荐答案

您的代码无法运行,因为testing的值不能满足您的任何需求案例陈述



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

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);


这篇关于使用IndexOf切换的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆