5

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!

2
  • So how does TrimStart handle values like -000012? Or will you always have positive integers? Commented Jul 27, 2011 at 18:41
  • yes, it will always be positive integers but I would agree using int.Parse or Int.TryParse is a better option. Commented Jul 27, 2011 at 18:47

7 Answers 7

7
int ret;

if (int.TryParse("0001234", out ret))
{
    return ret.ToString();
}

throw new Exception("eep");
Sign up to request clarification or add additional context in comments.

2 Comments

Cleanest code, TryParse is never as slick, but much higher quality than a straight parse.
Agree to that. But I believe that OP prefer a cast. +1
2
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();
    }));

Comments

1

Cast to integer and back to string?

int num    = Convert.ToInt32(val);
string val = Convert.ToString(num);

Comments

1

This should do the trick

string s = "0005";
string output = Convert.ToInt32(s.TrimStart(Convert.ToChar("0")));

Comments

1

The power of regular expressions to the rescue!

static readonly Regex rx = new Regex( @"[^\d]*((0*)([1-9][0-9]*)?)" ) ;
public static IEnumerable<string> ParseNumbers( string s )
{
  for ( Match matched = rx.Match(s) ; matched.Success ; matched = matched.NextMatch() )
  {
    if ( matched.Groups[1].Length > 0 )
    {
      yield return matched.Groups[3].Value ;
    }
  }
}

Passing the string "00001234, 002345667, 0000000, 011, 00000987" to ParseNumbers() yields the enumeration

  • "1234"
  • "2345667"
  • ""
  • "11"
  • "987"

Cheers!

Comments

0

000000 is 0 not null....

int intInteger = -1;
int.tryparse(stringNumber,out intInteger);

should give you a clean integer.

Comments

0

you can use Int.Parse to parse the string into an integer.

    String s = "00001234, 002345667, 0000000, 011, 00000987";
    string[] nums= s.Split(',');
foreach (string num in nums
{
    Console.WriteLine(Int.Parse(num)); //you'll need special handling to print "<no value>" when the string is 0000000
}

(haven't actually compiled this)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.