I need to split a string in C# as follows:
The string is something like this: 0000120400567
There are always 0
s at the beginning. In the example above there are six zeros followed by 120400567
. I want to split the string that I get the last part 120400567
.
The amount of zeros at the beginning might change (the last part number will increase by one number) that means we can have 001245878945
and what I want is 1245878945
.
How can I split this string leaving off the first 0 or the first x amount of zeros and get the end number only? It might be so that the number don't have any zero at the beginning and the number starts directly from the first number ... but it might be that the number contains 8 zeros and than 2 or more number of digits.
string withoutLeadingZeroes = withLeadingZeroes.TrimStart('0');
(or
string withoutLeadingZeroes = Regex.Replace(withLeadingZeroes, "^0*", "");
or
string withoutLeadingZeroes = new String(
withLeadingZeroes.SkipWhile(c => c == '0').ToArray());
or ...)
TrimStart
is your friend. However, you need to be careful that you don't end up with an empty string when your original consists of 0s only.
string s = "0000120400567";
s = s.TrimStart('0');
if (s == "")
s = "0";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With