使用 string.Contains() 和 switch() [英] Use string.Contains() with switch()

查看:36
本文介绍了使用 string.Contains() 和 switch()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个我使用的 C# 应用程序

I'm doing an C# app where I use

if ((message.Contains("test")))
{
   Console.WriteLine("yes");
} else if ((message.Contains("test2"))) {
   Console.WriteLine("yes for test2");
}

有什么办法可以将 if() 语句改为 switch() 语句吗?

There would be any way to change to switch() the if() statements?

推荐答案

可以先检查一下,然后随意使用开关.

You can do the check at first and then use the switch as you like.

例如:

string str = "parameter"; // test1..test2..test3....

if (!message.Contains(str)) return ;

然后

switch(str)
{
  case "test1" : {} break;
  case "test2" : {} break;
  default : {} break;
}

这篇关于使用 string.Contains() 和 switch()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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