简体   繁体   中英

Split string after specific character or after max length

i want to split a string the following way:

string s = "012345678x0123x01234567890123456789";
s.SplitString("x",10);

should be split into

012345678
x0123
x012345678
9012345678
9

eg the inputstring should be split after the character "x" or length 10 - what comes first.

here is what i've tried so far:

public static IEnumerable<string> SplitString(this string sInput, string search, int maxlength)
{
    int index = Math.Min(sInput.IndexOf(search), maxlength);
    int start = 0;
    while (index != -1)
    {
        yield return sInput.Substring(start, index-start);
        start = index;
        index = Math.Min(sInput.IndexOf(search,start), maxlength);
    }
}

I would go with this regular expression:

([^x]{1,10})|(x[^x]{1,9})

which means:

Match at most 10 characters that are not x OR match x followed by at most 9 characters thar are not x

Here is working example:

string regex = "([^x]{1,10})|(x[^x]{1,9})";
string input = "012345678x0123x01234567890123456789";
var results = Regex.Matches(input, regex)
                    .Cast<Match>()
                    .Select(m => m.Value);

which produces values by you.

Personally I don't like RegEx. It creates code that is hard to de-bug and is very hard to work out what it is meant to be doing when you first look at it. So for a more lengthy solution I would go with something like this.

    public static IEnumerable<string> SplitString(this string sInput, char search, int maxlength)
    {
        var result = new List<string>();
        var count = 0;
        var lastSplit = 0;

        foreach (char c in sInput)
        {
            if (c == search || count - lastSplit == maxlength)
            {
                result.Add(sInput.Substring(lastSplit, count - lastSplit));
                lastSplit = count;
            }

            count ++;
        }

        result.Add(sInput.Substring(lastSplit, count - lastSplit));

        return result;
    }

Note I changed the first parameter to a char (from a string). This code can probably be optimised some more, but it is nice and readable, which for me is more important.

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