Mar*_*ill 53
您可以使用IndexOf()方法,该方法采用StringComparison类型:
string s = "foobarbaz";
int index = s.IndexOf("BAR", StringComparison.CurrentCultureIgnoreCase); // index = 3
Run Code Online (Sandbox Code Playgroud)
如果未找到该字符串,则IndexOf()返回-1.
sco*_*kel 11
没有不区分大小写的版本.使用索引代替(或正则表达式虽然不推荐和矫枉过正).
string string1 = "my string";
string string2 = "string";
bool isContained = string1.IndexOf(string2, StringComparison.OrdinalIgnoreCase) >= 0;
Run Code Online (Sandbox Code Playgroud)
IndexOf通常用于更多"程序化"文本,如您可能已生成的路径或常量,并且是字符串比较的最快方法.对于语言使用的文本字符串StringComparison.OrdinalIgnoreCase或StringComparison.CurrentCultureIgnoreCase.
如果找到匹配项,包含返回一个布尔值。如果要搜索不区分大小写,可以在匹配之前使源字符串和字符串都匹配大写或小写。
例子:
if(sourceString.ToUpper().Contains(stringToFind.ToUpper()))
{
// string is found
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33465 次 |
| 最近记录: |