How to determine if string contains specific substring within the first X characters

I want to check whether Value1 below contains "abc" within the first X characters. How would you check this with an if statement?

var Value1 = "ddabcgghh";

if (Value1.Contains("abc"))
{
    found = true;
}

It could be within the first 3, 4 or 5 characters.


Solution 1:

Or if you need to set the value of found:

found = Value1.StartsWith("abc")

Edit: Given your edit, I would do something like:

found = Value1.Substring(0, 5).Contains("abc")

Solution 2:

I would use one of the of the overloads of the IndexOf method

bool found = Value1.IndexOf("abc", 0, 7) != -1;

Solution 3:

shorter version:

found = Value1.StartsWith("abc");

sorry, but I am a stickler for 'less' code.


Given the edit of the questioner I would actually go with something that accepted an offset, this may in fact be a Great place to an Extension method that overloads StartsWith

public static class StackOverflowExtensions
{
    public static bool StartsWith(this String val, string findString, int count)
    {
        return val.Substring(0, count).Contains(findString);
    }
}