0

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.

3
  • 2
    String.Format is your friend. MSDN documentation: msdn.microsoft.com/en-us/library/… Commented Nov 12, 2014 at 7:44
  • 1
    Or ToString: myIntValue.ToString("D8"); //8 means total length of output string Commented Nov 12, 2014 at 7:46
  • @SBI : Can you point to the constructor or the function or the format literals to use to maintain fixed length of numeric string with String.Format? Commented Nov 12, 2014 at 7:47

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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...

Comments

0

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 :)

Comments

0

..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

Comments

0

For modern .NET 5.0+ (2021 update)

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

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.