This might be really simple but i have a service that returns a string that has a number preceded with zeros. The count of zeros is not predictable but i need to extract the number out of the value. The length of the value is also not constant. For ex. 00001234, 002345667, 0000000, 011, 00000987
- in all these values, i need to extract the 1234, 2345667, <no value>, 11, 987
.
i have tried doing the below code but it returns the zeros as well:
string.Join( null,System.Text.RegularExpressions.Regex.Split( expr, "[^\\d]" ) );
Can anyone help?
Got my Answer::
I got it using stringObj.TrimStart('0')
. But i agree using Int.Parse or Int.TryParse is a better way of handling. Hope this is useful to someone like me!
int ret;
if (int.TryParse("0001234", out ret))
{
return ret.ToString();
}
throw new Exception("eep");
var numString = "00001234, 002345667, 0000000, 011, 00000987";
// result will contain "1234, 2345667, <no value>, 11, 987"
var result = string.Join(", ", numString.Split().Select(s =>
{
var intVal = int.Parse(s);
return intVal == 0 ? "<no value>" : intVal.ToString();
}));
Cast to integer and back to string?
int num = Convert.ToInt32(val);
string val = Convert.ToString(num);
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