user1240679
user1240679

Reputation: 6979

Creating fixed length numeric string

I have a certain number of files for which I need the filenames in my program. The files have a fixed naming fashion i.e. (prefix + digits).jpg. For e.g.: head001.jpg, head002.jpg, head003.jpg etc. etc.

The number of digits, in the end, can be varying - so the program has variables to change where the file naming starts from, where it ends and how many number digits are used in the naming. For e.g: A second scenario could be - tail00001.jpg, tail00002.jpg, tail00003.jpg etc. until tail00100.jpg
And in this case: start digit would be 0, end digit would be 100 and numDigits would be 5

In C++, I’ve seen this formatting being done as follows:
format <<prefix<<"%0"<<numDigits<<"d."<<filetype; //where format is a stringstream

However, I’m not quite sure about the best way to do this in C# and would like to know how to solve this.

Upvotes: 0

Views: 2953

Answers (5)

Robb Hoff
Robb Hoff

Reputation: 1910

For modern .NET 5.0+ (2021 update)

int myint = 100;
string zeroPadded = $"{myint:d8}";      // "00000100"
string spacePadded = $"{myint,8}";      // "     100"

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460048

..and in this case: start digit would be 0, end digit would be 100 and numDigits would be 5

You could use String.Format and the decimal format/precision specifier "D"` and a for-loop:

int start = 0;
int end = 100;
int numDigits = 5;
string name = "tail";
string extension = ".jpg";
for(int i = start; i <= end; i++)
{
    string fileName = string.Format(
        "{0}{1}{2}", name, i.ToString("D" + numDigits), extension);
    Console.WriteLine(fileName);
}

Outputs:

tail00000.jpg
tail00001.jpg
tail00002.jpg
tail00003.jpg
tail00004.jpg
tail00005.jpg
tail00006.jpg
tail00007.jpg
tail00008.jpg
tail00009.jpg
tail00010.jpg
....
tail100.jpg

Upvotes: 0

Steve Lillis
Steve Lillis

Reputation: 3256

You can do this using string.Format

var result = string.Format("{0}{1:00000}{2}", prefix, number, filetype)

Or you could use padleft

var result = prefix + number.ToString().PadLeft('0', numDigits) + "." + extension;

Or you can use a mix of the two :)

Upvotes: 0

Vano Maisuradze
Vano Maisuradze

Reputation: 5899

string has PadLeft method:

int n1 = 1;
string t1 = n1.ToString().PadLeft(5, '0'); // This will return 00001
int n10 = 10;
string t2 = n10.ToString().PadLeft(5, '0'); // This will return 00010 and so on...

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499860

Just use string.Format, with a precision specifier saying how many digits you want:

string name = string.Format("tail{0:d6}.jpg", index);

See the MSDN documentation for standard numeric string formats for more details.

You can build the string format up programmatically of course:

string name = string.Format("tail{0:d" + digits + "}.jpg", index);

Or use PadLeft as suggested by Vano. You might still want to use string.Format though:

string name = string.Format("tail{0}.jpg",
                            index.ToString().PadLeft(digits, '0'));

Using PadLeft has the advantage that it's easier to change the padding value, although I would imagine you'd always want it to be 0 anyway.

Upvotes: 7

Related Questions