Trim leading n alpha characters from a string

I need to trim the first n alpha characters from a string.

Examples:

a123456 → 123456
abc123456 → 123456
abc123456def → 123456def

+3
source share
2 answers

Try something like this:

String output = Regex.Replace(input, @"^[^\d]+", String.Empty);

Here's how the regex works:

^[^\d]+

^binds the expression to the beginning of the line - this is a set of characters that matches all non-integer values qualifies , matching it one or more times
[^\d] + [^\d]

So basically this regular expression matches all non-integer characters in the string until an integral character is found.

+9
source
static string AlphaTrimRight(string value)
{
    while (!Char.IsNumber(value[0]))
        value = value.Substring(1, value.Length - 1);
    return value;
}
+1

Source: https://habr.com/ru/post/1710574/


All Articles