检查字符串是否包含10个字符之一 [英] Check if a string contains one of 10 characters

查看:75
本文介绍了检查字符串是否包含10个字符之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#,我想检查一个字符串是否包含十个字符之一,*,&,#等,等等。

I'm using C# and I want to check if a string contains one of ten characters, *, &, # etc etc.

什么是最好的

推荐答案

在我看来,以下是最简单的方法:

The following would be the simplest method, in my view:

var match = str.IndexOfAny(new char[] { '*', '&', '#' }) != -1

或可能更易于阅读的形式:

Or in a possibly easier to read form:

var match = str.IndexOfAny("*&#".ToCharArray()) != -1

根据所需的上下文和性能,您可能会或可能不想缓存char数组。

Depending on the context and performance required, you may or may not want to cache the char array.

这篇关于检查字符串是否包含10个字符之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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