OrElse
OrElse

Reputation: 9959

How can i create a dynamic string format in C#

I have as input the string format CST-000000 and an integer with value 1

Upon using

string result = string.Format("CST-000000", 1);

the expected result should be CST-000001 instead of CST-000000

How could i create this string format dynamically? For example

 - CST-000 should produce CST-001
 - HELLO000 should produce HELLO001
 - CUSTOMER0000 should produce CUSTOMER0001

Upvotes: 0

Views: 973

Answers (4)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112259

String.Format requires a placeholder {n} with a zero-based argument number. You can also add it a format {n:format}.

string result = String.Format("CST-{0:000000}", 1);

You can also use String interpolation

string result = $"CST-{1:000000}"

The difference is that instead of a placeholder you specify the value directly (or as an expression). Instead of the Custom numeric format string, you can also use the Standard numeric format string d6: $"CST-{1:d6}"


If you want to change the format template dynamically, String.Format will work better, as you can specify the format and the value as separate arguments.

(Example assumes an enum FormatKind and C# >= 8.0)

int value = 1;
string format = formatKind switch {
    FormatKind.CstSmall => "CST-{0:d3}",
    FormatKind.CstLarge => "CST-{0:d6}",
    FormatKind.Hello => "HELLO{0:d3}",
    FormatEnum.Customer => "CUSTOMER{0:d4}"
};
string result = String.Format(format, value);

Also note that the value to be formatted must be of a numeric type. Strings cannot be formatted.

See also: Composite formatting

Upvotes: 3

AFract
AFract

Reputation: 9723

ToString and String.Format can do much more than use predefined formats.

For example :

string result = string.Format("CST-{0:000000}", 1);

string result = 1.ToString("CST-000000");

Should both do what you want.

(Of course you could replace "1" by any variable, even a decimal one).

Upvotes: 0

OrElse
OrElse

Reputation: 9959

It seems .toString("CST-000"), .toString("HELLO000") and so on, does the trick.

Upvotes: 0

canton7
canton7

Reputation: 42225

Assuming that:

  • You receive your format string from somewhere and you can't control what it looks like
  • Your format string ends with 1 or more zeros
  • If the format string is e.g. CST-00000 and your value is 123, you want the result to be CST-00123

You can do something like this:

Inspect your format string, and separate out the stuff at the beginning from the zeros at the end. It's easy to do this with Regex, e.g.:

string format = "CST-000000";
// "Zero or more of anything, followed by one or more zeros at the end of the string"
var match = Regex.Match(format, "(.*?)(0+)$");
if (!match.Success)
{
    throw new ArgumentException("Format must end with one or more zeros");  
}
string prefix = match.Groups[1].Value; // E.g. CST-
string zeros = match.Groups[2].Value; // E.g. 000000

Once you have these, note the "Zero placeholder" in this list of custom numeric format strings -- you can write e.g. 123.ToString("0000") and the output will be 0123. This lets you finish off with:

int value = 123;
string result = prefix + value.ToString(zeros);

See it on dotnetfiddle

Upvotes: 3

Related Questions