【发布时间】:2011-05-01 12:27:28
【问题描述】:
希望有人可以帮助我。我只是在学习 C#,我有一个简单的问题。
我有一个变量,我想检查它是否存在于另一个字符串中。像
if ( test contains "abc" ) {
}
在 C# 中有没有简单的方法来做到这一点
【问题讨论】:
-
Geepers...您几乎回答了您的问题。使用 API 文档卢克......力量只是 90 年代
标签: c#
希望有人可以帮助我。我只是在学习 C#,我有一个简单的问题。
我有一个变量,我想检查它是否存在于另一个字符串中。像
if ( test contains "abc" ) {
}
在 C# 中有没有简单的方法来做到这一点
【问题讨论】:
标签: c#
if (stringValue.Contains(anotherStringValue))
{
// Do Something //
}
【讨论】:
IndexOf() 函数将完成工作...
如果字符串不存在则返回-1
【讨论】:
string MainString = "String Manipulation";
string SearchString = "pul";
int FirstChr = MainString.IndexOf(SearchString);
此代码显示如何在字符串中搜索子字符串,并返回开始的索引位置或表示未找到该字符串的 -1。
你也可以使用Contains(),Contains 是字符串类型的一个实例方法,这意味着你可以在你的程序中对特定的字符串调用它。它有一个bool结果,如果找到参数则为true,如果未找到则为false。
using System;
class Program
{
static void Main()
{
Test("Dot Net Perls");
Test("dot net perls");
}
static void Test(string input)
{
Console.Write("--- ");
Console.Write(input);
Console.WriteLine(" ---");
//
// See if the string contains 'Net'
//
bool contains = input.Contains("Net");
//
// Write the result
//
Console.Write("Contains 'Net': ");
Console.WriteLine(contains);
//
// See if the string contains 'perls' lowercase
//
if (input.Contains("perls"))
{
Console.WriteLine("Contains 'perls'");
}
//
// See if the string contains 'Dot'
//
if (!input.Contains("Dot"))
{
Console.WriteLine("Doesn't Contain 'Dot'");
}
}
}
检查C# String Functions and Manipulation 了解有关字符串的任何信息。
【讨论】:
使用String.Contains(...) 可能不是一个好主意。
String.Contains(...) 进行区分大小写的顺序比较。因此,请注意大小写匹配。
当然你可以在检查前使用ToLower() 或ToUpper()
【讨论】:
if (stringValue.ToUpper().Contains("FIND_THIS"))
{
// Do Something //
}
是不区分大小写搜索的另一个很好的变体。
【讨论】:
请参考This。
String.Contains(...)
【讨论】:
您必须使用正则表达式。例如Regex.IsMatch(test, "abc")。如果 test 包含 abc,这将返回 true。
【讨论】:
为此可以使用String.Contains。
if (test.Contains("abc"))
{
// Your Code Here
}
【讨论】:
有一些方法可以进行此验证,例如 CompareTo、Contains、Compare、IndexOfAny 和 IndexOf。
查看String类的方法列表。
string s1 = "ani\u00ADmal";
string s2 = "animal";
string s3 = "abc";
string s4 = "abc";
string s5 = "ABC";
bool b1 = s1.CompareTo(s2) > -1; // return true, exists
bool b2 = s3.CompareTo(s4) > -1; // return true, exists
bool b3 = s3.CompareTo(s5) > -1; // return false, no case sensitive, no exists
bool b4 = s1.Contains(s2); // return false, no exists
bool b5 = s3.Contains(s4); // return true, exists
bool b6 = s3.Contains(s5); // return false, no case sensitive, no exists
string s6 = "MACHINE";
string s7 = "machine";
string s8 = "nature";
int a = String.Compare(s6, 0, s7, 0, s6.Length, true); // return 0, contain and is less
int b = String.Compare(s6, 0, s7, 0, s6.Length, false); // return 1, contain and is greater
int c = String.Compare(s6, 0, s8, 0, s6.Length, true); // return -1, no contain
int d = String.Compare(s6, 0, s8, 0, s6.Length, false); // return -1, no contain
int e = s1.IndexOfAny(s2.ToCharArray()); // return 0, exists
int f = s3.IndexOfAny(s4.ToCharArray()); // return 0, exists
int g = s3.IndexOfAny(s5.ToCharArray()); // return -1, no case sensitive, no exists
int h = s1.IndexOf(s2); // return 0, exists
int i = s3.IndexOf(s4); // return 0, exists
int j = s3.IndexOf(s5); // return -1, no exists
【讨论】:
您可以使用.Contains(),但也可以使用.ToUpper(),因为.Contains() 区分大小写。
if(string1.ToUpper().Contains(string2.ToUpper())
{
//Your code goes here
}
【讨论】: