[英]How to check if string contains the text in C# Winforms
Can anyone help me in writing a piece of code in C# Winforms, which can give me a boolean value, that whether a piece of string contains a few words. 谁能帮我用C#Winforms编写一段代码,该代码可以给我一个布尔值,即一段字符串是否包含几个单词。
For example, If I want to check whether string test only string exists in string "Data is provided to test only string". 例如,如果我要检查字符串是否存在串仅测试串 “的数据提供给测试只串”。
I have written following piece of code, but it alwys gives me true, whether string contains the words or nor. 我已经编写了以下代码,但是无论字符串包含单词还是否,它都使我成真。
private bool ContainsText(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (((int)input[i] >= 65 && (int)input[i] <= 90) || ((int)input[i] >= 97 && (int)input[i] <= 177))
return true;
}
return false;
}
When I call the following line, I always get true , which is not correct 当我调用以下行时,我总是得到true ,这是不正确的
MessageBox.Show(ContainsText("test only string").ToString());
Code-wise, your ContainsText
code immediately returns true
if any character in input
is either in "A to Z" or "a to U+00B1" for some reason. 在代码方面,如果由于某种原因
input
任何字符位于“ A到Z”或“ a到U + 00B1”中,您的ContainsText
代码将立即返回true
。
But the problems lie deeper than that: you've described two inputs - the string to check for, eg "test only string" and the string to check for its presence, eg "Data is provided to test only string". 但是问题还不止于此:您已经描述了两个输入-要检查的字符串(例如“仅测试字符串”)和要检查其存在的字符串(例如“提供数据以仅测试字符串”)。 Your method only accepts one input, and doesn't use any other state.
您的方法仅接受一个输入,而不使用任何其他状态。 So it can't possibly work.
因此它不可能工作。 It's worth taking a step back and trying to work out why you didn't notice that you needed two inputs - and why indeed your test only used "test only string" and didn't mention "Data is provided to test only string".
值得退后一步,尝试弄清为什么您没有注意到需要两个输入-以及为什么实际上您的测试仅使用“仅测试字符串”,却没有提及“提供数据以仅测试字符串”。
You don't really need a method at all though - String
already has a Contains
method: 您实际上根本不需要任何方法-
String
已经具有一个Contains
方法:
if (textToCheck.Contains(textToFind))
{
...
}
That's assuming you want an ordinal comparison. 假设您要进行序数比较。 Use
IndexOf
with an appropriate StringComparison
if you want to check in a culture-sensitive or case-insensitive way. 如果要以区分文化或不区分大小写的方式进行检查,请结合使用
IndexOf
和适当的StringComparison
。
A simple string.IndexOf perhaps with the enum to ignore case: 一个简单的string.IndexOf可能带有枚举来忽略大小写:
string myTestString = "Data is provided to Test Only String";
if(myTestString.IndexOf("test only string", StringComparison.CurrentCultureIgnoreCase) >= 0)
Console.WriteLine("Found text");
Of course the string class has also a Contains method, but it is implemented as a call to IndexOf, without the possibility to ignore the case. 当然,字符串类也有一个Contains方法,但是它是作为对IndexOf的调用实现的,不可能忽略大小写。
public bool Contains(string value)
{
return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}
You can use Contains method of String object. 您可以使用String对象的Contains方法。
var a = "Data is provided to test only string";
var b = "test only string";
if (a.Contains(b))
MessageBox.Show("yes");
Use IndexOf 使用IndexOf
http://www.dotnetperls.com/indexof http://www.dotnetperls.com/indexof
example of usage.. 使用示例
string str = "string to test";
bool result = str.IndexOf("The hosted network started.") != -1;
MessageBox.Show(result.ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.