简体   繁体   English

Selenium C#:字符串包含

[英]Selenium C#: string contains

I have something like:我有类似的东西:

string result = Selenium.GetText("/html/body/form/div[2]");
if (result.Contains("test")
{
   bool found = true;
}
else
{
   found = false;
}

My problem is using result.Contains() returns false if there are tests, testing, etc. Also returns false for uppercase TEST, Test, etc我的问题是使用result.Contains()如果有测试、测试等返回 false。对于大写 TEST、Test 等也返回 false

Is there another method that would match each character?是否有另一种方法可以匹配每个字符? Something like: result.Match("test");类似: result.Match("test");

Thanks for helping me out.谢谢你的协助。

string.StartsWith is a good start, and then Regex if you need more power string.StartsWith是一个好的开始,如果你需要更多的力量,然后是Regex

Pardon my awful code, though it works:请原谅我糟糕的代码,尽管它有效:

var aStartsWithB = stringA.ToUpper().StartsWith(stringB.ToUpper());
var aContainsB = stringA.ToUpper().Contains(stringB.ToUpper());

contains it should work fine:包含它应该可以正常工作:

public static void Main()
{
    string result = @"/html/body/form/tests123456";
    var containsTest= result.Contains("test"); // <--True
}

Just bear in mind that Contains is case sensitive请记住,包含区分大小写

You could use a version of string.Contains case insensitive as showed on thepost below.您可以使用 string.Contains 不区分大小写的版本,如下面的帖子所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM