56

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.

1
  • Sorry guys I wasn't clear when I first posted, the value abc (this changes) maybe within the first X number of characters. E.g. 3,4,5 etc so StartsWith won't work for me. Commented Feb 1, 2009 at 16:06

10 Answers 10

57

I would do something like:

found = Value1.Substring(0, 5).Contains("abc")
Sign up to request clarification or add additional context in comments.

1 Comment

Also add a check to see if the Value1 String has a length of 5 before doing this.
19

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

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

Comments

12

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);
    }
}

Comments

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

3 Comments

I don't think that meets the requirements.
Isn't that the same as found = Value1.StartsWith("abc") ?
@Will - true but as in all software projects the requirements have been changed since I had a go at it. @Jim - True but I kept the style of the author of question.
7

Use IndexOf is easier and high performance.

int index = Value1.IndexOf("abc");
bool found = index >= 0 && index < x;

1 Comment

But less efficient if Value1 is a long string and x is small.
3

This is what you need :

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

Comments

3

A more explicit version is

found = Value1.StartsWith("abc", StringComparison.Ordinal);

It's best to always explicitly list the particular comparison you are doing. The String class can be somewhat inconsistent with the type of comparisons that are used.

Comments

3

You can also use regular expressions (less readable though)

string regex = "^.{0,7}abc";

System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex);
string Value1 = "sssddabcgghh";

Console.WriteLine(reg.Match(Value1).Success);

1 Comment

This works but the regular expression is inefficient. It will grab the first 7 characters and then test for "abc". If that test fails it will back up one character and try again. This process will continue until either a match is found or the beginning of the string is reached. The non-greedy equivalent is "^.{0,7}?abc".
2

You're close... but use: if (Value1.StartsWith("abc"))

Comments

2

Adding on from the answer below i have created this method:

    public static bool ContainsInvalidStrings(IList<string> invalidStrings,string stringToCheck)
    {

        foreach (string invalidString in invalidStrings)
        {
            var index = stringToCheck.IndexOf(invalidString, StringComparison.InvariantCultureIgnoreCase);
            if (index != -1)
            {
                return true;
            }
        }
        return false;
    }

it can be used like this:

            var unsupportedTypes = new List<string>()
        {
            "POINT Empty",
            "MULTIPOINT",
            "MULTILINESTRING",
            "MULTIPOLYGON",
            "GEOMETRYCOLLECTION",
            "CIRCULARSTRING",
            "COMPOUNDCURVE",
            "CURVEPOLYGON",
            "MULTICURVE",
            "TRIANGLE",
            "TIN",
            "POLYHEDRALSURFACE"
        };


            bool hasInvalidValues =   ContainsInvalidStrings(unsupportedTypes,possibleWKT);

you can check for multiple invalid values this way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.