-6

I need to check if the first chars of a string are digits and I tried it with the following:

if(char.IsDigit(value[0]))
      value = value.Remove(0,1);

That will work for strings like this: "1ABCDEF".

But the problem arises when the string looks like the following: "1234ABC"?

How can I remove every digit at the beginning? Update: I only want to remove the digits in the beginning, not all! When the string looks like this: "123ABCD123123" the string after the check should look like this: "ABCD123123"

InBetween
  • 32,319
  • 3
  • 50
  • 90
kb_
  • 620
  • 4
  • 21

2 Answers2

3

How can I remove every digit at the beginning?

Linq SkipWhile would be one way

string result = string.Concat("1234ABC123".SkipWhile(char.IsDigit)); // "ABC123"
fubo
  • 44,811
  • 17
  • 103
  • 137
1

Learning one liners with Linq is great, but maybe you should get a handle on the basics first. If you are asking how to perform this rather trivial task then its because you are just learning. Dont jump further than you should, baby steps is the safest way to learn.

string has a handy little method named TrimStart that trims whitespaces or any custom characters if specified.

So one option would be:

var digits = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
value = value.TrimStart(digits);

UPDATE As pointed out in fubo's commentary, a very clean alternative to this approach is:

value = value.TrimStart("0123456789".ToCharArray());

END UPDATE

But that seems a bit cumbersome, so you could consider implementing your own custom method (no task is small enough to not merit its own method):

public string TrimStartIfDigit(string s)
{
    int index;

    for (index = 0; index < s.Length; index++)
    {
        if (!char.IsDigit(s[0]))
            break;
    }

    return index == 0 ? s : s.Substring(index); 
}

And you could call it like value = TrimStartIfDigit(value);. That looks much better, but we can still do better. Welcome to extension methods and c#'s wonderful world of syntactic sugar.

public string TrimStartIfDigit(this string s)
{
    int index;

    for (index = 0; index < s.Length; index++)
    {
        if (!char.IsDigit(s[0]))
            break;
    }

    return index == 0 ? s : s.Substring(index); 
}

And now the callsite reads even better: value = value.TrimStartIfDigit();.

Great, but hey, this seems like a handy little method, we could generalize this to any characters worth trimming and maybe we can improve the original syntax of TrimStart and the unweildly array of characters. Welcome to the land of lambdas!

public static string TrimStart(this string s, Predicate<char> trimIf)
{
    int index;

    for (index = 0; index < s.Length; index++)
    {
        if (!trimIf(s[index]))
            break;
    }

    return index == 0 ? s : s.Substring(index);
}

And the callsite doesn't look too bad at all: value = value.TrimStartIfDigit(c => char.IsDigit(c));

Once you get the hold of all these nuances and you understand whats going on and how its done more or less, start using Linq.

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • 1
    `value = value.TrimStart("0123456789".ToCharArray());` +1 probably the cleanes approach – fubo Dec 12 '17 at 15:26
  • @fubo ha! true! Missed that one, but my excuse is that its still too early to use clever tricks ;). – InBetween Dec 12 '17 at 15:31