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.