Having another tough one here guys, basically I would like to check if a string contains the word “Foo” and if it does contain it, does it start with it? If it does start with Foo, it should be the only Foo that starts with a capital, all others should be small letters.
If the above criteria is met, it should return true.
If the string contains Foo but doesn’t start with Foo, it should immediately return false since you cant have a capital Foo in the middle of the string.
In the event that said string contains foo, but doesn’t start with Foo, all instances of foo should be lowercase. If this criteria is met, return true.
I should mention i am looking for C# code, I tried and have not yet succeeded, but since I have only been programming for 2 weeks, I don’t think this will be trouble for some of you season pro’s.
This is what I have tried as requested, I thinks its faaar off but at least I tried.
if (Title.Contains("Foo") == true && Regex.IsMatch(Title, "^Foo") == true)
{
CheckAnd = true;
}
else if (Title.Contains("Foo") == true && Regex.IsMatch(Title, "^Foo") == false)
{
CheckAnd = false;
}
else if (Regex.IsMatch(Title, "^foo"))
{
CheckAnd = false;
}
else
{
CheckAnd = true;
}
Ok guys, nearly there, this is what I got from all your answers:
if (Title.IndexOf("Foo") == 0 && Title.LastIndexOf("Foo") == 0)
{
CheckAnd = true;
}
else if (Title.LastIndexOf("Foo") > 0)
{
CheckAnd = false;
}
else if(Title.Contains("foo") && Title.StartsWith("Foo") == false && PUT CHECK HERE)
The last thing I need to check that all occurrences of foo are lowercase in the last else if statement ?
If I undestood it correctly this should do it !