3

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

Is there another method that would match each character? Something like: result.Match("test");

Thanks for helping me out.

Ross Patterson
  • 9,527
  • 33
  • 48
Maya
  • 7,053
  • 11
  • 42
  • 53
  • 3
    result.Contains("test") will return true if result contains "tests" or "testing" because each of the words contains "test". – Wesley Wiser Jul 22 '11 at 15:43
  • @ Wesley Wiser: really? I did not think so. Let me try again. – Maya Jul 22 '11 at 15:45
  • 1
    `result.Contains("test")` will return true if "test" occurs anywhere in `result`, regardless of whether it's a whole or partial word. If it returns false, then "test" doesn't occur anywhere in result. But note that this is a case-sensitive search. Are you sure the case matches? – Igby Largeman Jul 22 '11 at 15:47
  • @ Charles: Thanks, I tried again, and figured that it was a case issue. I used ToUpper, and it is returning true. Thanks again Wesley and Charles! – Maya Jul 22 '11 at 15:51

3 Answers3

2

string.StartsWith is a good start, and then Regex if you need more power

kͩeͣmͮpͥ ͩ
  • 7,783
  • 26
  • 40
1

Pardon my awful code, though it works:

var aStartsWithB = stringA.ToUpper().StartsWith(stringB.ToUpper());
var aContainsB = stringA.ToUpper().Contains(stringB.ToUpper());
Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111
1

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 the post below.

Community
  • 1
  • 1
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70