How to pad left a number with a specific amount of zeroes
Asked Answered
S

3

43

I want to pad left every number with zeroes (it has to be 8 digits) in my string.

e.g.

asd 123 rete > asd 00000123 rete
4444 my text > 00004444 my text

Is it possible to do this using regular expressions? Especially Regex.Replace()?

Notice that the number of zeroes is different for different numbers. I mean the padded number has to be 8 digits long.

Seating answered 10/8, 2012 at 12:10 Comment(2)
Come on... a little googling wouldn't hurt, and you don't need regex for this: msdn.microsoft.com/en-us/library/66f6d830%28VS.71%29.aspxSlew
Yeah, I know about PadLeft() method, but the problem is I have a string with some strange words inside and I want to add zeroes only to numbers (and as u can notice the number of these zeroes is different for different numbers). I can use Regex to find all the numbers and check its length, but I wonder if I can do this easier with Replace()?Seating
R
115

C# has the built-in PadLeft() function for this:

someString = someString.PadLeft(8, '0');

And here's an article on it at Microsoft Learn.

To use a regular expression, do something like this:

string someText = "asd 123 rete"; 
someText = Regex.Replace(someText, @"\d+", n => n.Value.PadLeft(8, '0'));
Reprehension answered 10/8, 2012 at 12:13 Comment(3)
I asked about something different:P I don't want to split my string into an array to pad left the chunks. I want to do this in one line using regular expression;)Seating
This doesn't perform any splitting at all.Gammadion
And also you can use PadRight method to fix the length of a string by adding a special character in the right side of it.Dado
B
6

The OP states they want to use a regular expression. I had to do a inline replace in SQL so some home made SQL functions that calls a C# regex has been helpful.

What I needed to pad looked something like this:

abc 1.1.1
abc 1.2.1
abc 1.10.1

and I wanted:

abc 001.001.001
abc 001.002.001
abc 001.010.001

So I could sort it alphabetically.

The only solution so far (that I found) was to do the padding and truncating to right length in two steps. I couldn't use a Lambda since this was in SQL and I hadn't prepared my functions for that.

//This pads any numbers and truncates it to a length of 8
var unpaddedData = "...";
var paddedData = Regex.Replace(unpaddedData , "(?<=[^\d])(?<digits>\d+)",
                                                     "0000000${digits}");
var zeroPaddedDataOfRightLength = Regex.Replace(paddedData ,"\d+(?=\d{8})","");

Explanations:

(?<=[^\d])(?<digits>\d+)
(?<=[^\d])       Look behind for any non digit, this is needed if there are 
                 more groups of numbers that needs to be padded
(?<digits>\d+)   Find the numbers and put them in a group named digits to be 
                 used in the replacement pattern

0000000${digits} Pads all the digits matches with 7 zeros

\d+(?=\d{8})     Finds all digits that are followed by at exactly 8 digits. 
                 ?= Doesn't capture the 8 digits.

Regex.Replace(...,"\d+(?=\d{8})","")   
                 Replaces the leading digits with nothing leaving the last 8.
Berezniki answered 8/7, 2014 at 8:19 Comment(0)
W
-2
static void Main(string[] args)
    {
       string myCC = "4556364607935616";
       string myMasked = Maskify(myCC);
       Console.WriteLine(myMasked);
    }        
    
public static string Maskify(string cc)
    {
       int len = cc.Length;
       if (len <= 4)
          return cc;
    
       return cc.Substring(len - 4).PadLeft(len, '#');
    }

Output:

############5616

Just replace the '#' with 0 or '0'.

Whacking answered 26/12, 2019 at 19:55 Comment(2)
To understand this site better please take the tour and then read some of the help center pages.Pushed
This doesn't appear to add anything beyond the existing answers.Riyadh

© 2022 - 2024 — McMap. All rights reserved.