I've got a string of digits that is either 4 or 5 digits long and it needs to be padded with "0" till it's 6 digits long. Is this possible? I'm using .Net framework.
views:
22answers:
1
+3
A:
You don't need a regular expression to perform this operation. You can use string.PadLeft
:
s = s.PadLeft(6, '0');
If you need to use regular expression (perhaps because you are performing some more complex replacement of which this is just a small part) then you can use a MatchEvaluator in combination with the above technique:
string s = "foo <12423> bar";
s = Regex.Replace(s, @"<(\d+)>", match => match.Groups[1].Value.PadLeft(6, '0'));
Result:
foo 012423 bar
Mark Byers
2010-05-20 13:25:46
Thanks. I was afraid it wasn't possible.
norbertB
2010-05-20 13:39:19