简体   繁体   中英

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.

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")

我会使用IndexOf方法的一个重载

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

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);
    }
}
if (Value1.StartsWith("abc")) { found = true; }

Use IndexOf is easier and high performance.

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

This is what you need :

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

你很接近......但是使用: if (Value1.StartsWith("abc"))

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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