0
点赞
收藏
分享

微信扫一扫

C#中怎样判断字符串中是否包含字符串(不区分大小写)


场景

在winform中连接mysql数据库并执行相应的sql,需要判断sql字符串中是否指定的字符串比如

删表的关键字drop database等,而且还得不区分大小写,即包含大写和小写的都不被允许。

注:

实现

1、通过ToLower()将要比较的字符串全部转换为小写,然后进行比较

通过字符串的Contains方法判断字符串中是否包含另一个字符串。

sql.ToLower().Contains(arr.ToLower())

2、完整示例方法

public static bool checkSql(string sql)
{
bool isRight = true;
string[] notAllowKeyWords = { "drop", "drop database" , "drop table" , "truncate", "alter","rename" , "create" };
for (int i = 0; i < notAllowKeyWords.Length; i++)
{
string arr = notAllowKeyWords[i];
if (sql.ToLower().Contains(arr.ToLower())) {
isRight = false;
}
}
return isRight;
}

举报

相关推荐

0 条评论