I have to manage a particular string formatting/padding condition. To be short I need to pad a string argument only if the argument length is 0. If I use the typical aligment parameter the padding is made if the length of the argument is smaller then the provided alignment value. For example:
string format = "#{0,10}#";
string argument;
string output;
argument = Console.ReadLine();
output = String.Format(format, argument);
String.WriteLine(output);
If I enter "try" as a value I got this result:
#try #
If I enter "trytrytrytry" I got:
#trytrytrytry#
What I would like to happen is depicted below:
Entering "" I would like to have:
# #
but entering "try" I would like to have:
#try#
The code I'm going to write should be as much generic as possibile since the format parameter is not static and is defined at runtime.
The best practice to do this is to define a custom string formatter. Unluckly it seems that the customization code can only act on the 'format' portion of the format parameter of the String.Format() method.
Indeed If I define a custom formatter:
public class EmptyFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if (!this.Equals(formatProvider))
return null;
if (string.IsNullOrEmpty(format))
throw new ArgumentNullException();
return numericString;
}
}
The format and arg parameters of the Format method didn't contain the alignment parameter then I cannot actually check what lenght of the padding value should be applied and therefore I cannot act properly. Moreover this part of the 'formatting' of the string seems to be applied somewhere else but I have not idea where.
Is there a way to 'alter' this behaviour ?
A format item has the following syntax:
index[,alignment][ :formatString] }
The reason the format parameter is null is because there is no formatString component, there is only alignment. I couldn't find a way to access the alignment component from the Format method. However, one (ugly) solution is to set the formatString to be the same as the alignment, so that you can access it using the format parameter:
#{0,10:10}#
A less ugly solution would be to create your own method extension that first extracts the alignment from the given format and then calls the traditional String.Format method.
For example:
public static string StringFormat(this string Arg, string Format) {
//extract alignment from string
Regex reg = new Regex(#"{[-+]?\d+\,[-+]?(\d+)(?::[-+]?\d+)?}");
Match m = reg.Match(Format);
if (m != null) { //check if alignment exists
int allignment = int.Parse(m.Groups[1].Value); //get alignment
Arg = Arg.PadLeft(allignment); //pad left, you can make the length check here
Format = Format.Remove(m.Groups[1].Index - 1, m.Groups[1].Length + 1); //remove alignment from format
}
return (string.Format(Format, Arg));
}
To use the code:
string format = "#{0,10}#";
string argument = "try";
string output = argument.StringFormat(format); //"# try#"
Related
I'm beginer in C#. Now I have next task: In method I get template and arguments and I have to return formated string.
For example:
template = "Hello, {name}!"
name = "Bob"
So result must be a string -> Hello, Bob!
public static string GetHelloGreeting(string template, string name)
{
return string.Format(template, name);
}
String.Format expects an index in the braces. You want to pass the name in it, so you can replace it with the actual name value.
I'd suggest to use String.Replace:
public static string GetHelloGreeting(string template, string name)
{
return template.Replace("{name}", name);
}
You could provide a method which is more reusable. For example:
public static string ReplaceAll(string template, params (string key, string value)[] replacements)
{
foreach (var kv in replacements)
{
template = template.Replace("{"+ kv.key + "}", kv.value);
}
return template;
}
Your example:
string res = ReplaceAll("Hello, {name}!", ("name", "Bob"));
but also possible with multiple parameters:
string res = ReplaceAll("Hello, {name}! Now it's {time}", ("name", "Bob"), ("time", DateTime.Now.ToString("HH:mm")));
The value of your template parameter will have to change somehow. If you want to use string interpolation, this answer shows that. So
template = $"Hello, {name}"; in which case you wouldn't need to use String.Format at all. Just make sure you define name before you define template.
Or you could use String.Format(template, name); like you have but you would need template = "Hello, {0}!";
The 0 is the index of the variable that will go in that position. For more info see String.Format
when specifying a format you use an index for the parameters that will follow. It is called a composite format string:
string template = "Hello, {0}!"
this makes it independent of variable names. But the true reason is that the overload of the Format method that you are using takes a params array as parameter as you can see in the method signature:
public static string Format (string format, params object?[] args);
so the index that is found in the template will be applied to extract the objects on the appropriate places from the array of objects that you pass into the method
If you want to use string.Format(), the template must be correct. Add the character $ at the beginning of the template:
try this:
string name = "Bob";
string template = $"Hello, {name}!";
Console.WriteLine(GetHelloGreeting(template, name)); // Hello, Bob!
public static string GetHelloGreeting(string template, string name)
{
return string.Format(template, name);
}
reult:
Hello, Bob!
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
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
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
It seems .toString("CST-000"), .toString("HELLO000") and so on, does the trick.
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).
I need to make a possibility to open a webpage either with an additional parameter or without one. If it has a parameter, I'll add it with the string.format function. Sometimes I need to format this parameter before opening the url, however, I don't want to make the code too specific.
The Parameter is always a string and I would need to cut the end of this string. Is it possible to do something in the string to be formatted to cut the unneeded text? If so how would the {0} have to look that it works?
string url = "http://foo.bar/xt:{0}";
string parameter = "abcdefghi";
if (Regex.Matches(Regex.Replace(url,
#"(\{{2}|\}{2})", ""), // removes escaped curly brackets
#"\{(\d+)(?:\:?[^}]*)\}")
.OfType<Match>().Any())
{
Process.Start(string.Format(url, parameter));
}
else
{
Process.Start(url);
}
Instead of abcdefghi I would like to have as parameter just abcdefg for instance. But this should be configurable via url. Something like {0:7) or so...
You have to write this kind of logic yourself. What you can do, however, is wrap this in an extension method so you don't have to duplicate it everywhere:
public static class StringExt
{
public static string Truncate(this string value, int maxLength)
{
if (string.IsNullOrEmpty(value)) return value;
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}
}
Now we can write:
var someString = "...";
someString = someString.Truncate(2);
In C#, I have a width I want to use for some strings, but I won't know that width until runtime. I'm doing something like this:
string.Format("{0, " + digits + "}", value) // prints 123 as " 123"
Is there a string formatting directive that lets me specify this without smashing my own format string together like this?
I looked around on MSDN for a little while and I feel like I'm missing a whole chapter on format strings or something.
Take a look at PadLeft:
s = "123".PadLeft(5); // Defaults to spaces
s = "123".PadLeft(5, '.'); // Pads with dots
You can use the PadLeft and PadRight methods:
http://msdn.microsoft.com/en-us/library/system.string.padleft%28VS.71%29.aspx
you can do something like
string test = valueString.PadLeft(10,' ');
or even sillier
string spaces = String.Concat(Enumerable.Repeat(" ", digits).ToArray());
The functions mentioned by others will work, but this MSDN page has a more general solution to formatting that changes at runtime:
Composite Formatting
They give examples much like yours.
Edit: I thought you were trying to solve the general case of composing a format string at runtime. For example, if there were no built in PadLeft(), you could do this:
int myInt = 123;
int nColumnWidth = 10;
string fmt = string.Format("Price = |{{0,{0}}}|", nColumnWidth);
// now fmt = "Price = |{0,5}|"
string s = string.Format(fmt, myInt);
You can even do all that in one line, but it's ugly:
string s = string.Format(
string.Format("Price = |{{0,{0}}}|", nColumnWidth),
myInt);
Perhaps this will help with your research on formatting:
Formatting Types
Composite Formatting
However, I don't think you're going to do much better than this, as the alignment parameter must be part of the format string and does not seem to be represented by a property.
Probably overkill but just to illustrate a way to encapsulate the format specification and use an overload of String.Format that accepts an IFormatProvider.
class Program
{
public static void Main(string[] args)
{
int digits = 7;
var format = new PaddedNumberFormatInfo(digits);
Console.WriteLine(String.Format(format, "{0}", 123));
}
}
class PaddedNumberFormatInfo : IFormatProvider, ICustomFormatter
{
public PaddedNumberFormatInfo(int digits)
{
this.DigitsCount = digits;
}
public int DigitsCount { get; set; }
// IFormatProvider Members
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
return null;
}
// ICustomFormatter Members
public string Format(string format, object arg, IFormatProvider provider)
{
return String.Format(
String.Concat("{0, ", this.DigitsCount, "}"), arg);
}
}
I posted a CodeProject article that may be what you want.
See: A C# way for indirect width and style formatting.
Basically it is a method, FormatEx, that acts like String.Format, except it allows for indirect alignment and formatString specifiers.
FormatEx("{0,{1}:{2}}", value, width, formatString);
Means format the value of varArgs 0, in a field width specified by varArgs 1, using a formattingString code specified by varArgs 2.
Edit: Internally, it does what many others have suggested in their answers. I've just wrapped the parsing and determination of the final values to use for alignment and formatString. I also added a "center alignment" modifier.
-Jesse
String has a constructor that creates a string with a given character repeated n times.
https://msdn.microsoft.com/en-us/library/xsa4321w(v=vs.110).aspx
// prints 123 as " 123"
string.Format(new string(' ', digits) + "{0}", value)
I am trying to create a generic formatter/parser combination.
Example scenario:
I have a string for string.Format(), e.g. var format = "{0}-{1}"
I have an array of object (string) for the input, e.g. var arr = new[] { "asdf", "qwer" }
I am formatting the array using the format string, e.g. var res = string.Format(format, arr)
What I am trying to do is to revert back the formatted string back into the array of object (string). Something like (pseudo code):
var arr2 = string.Unformat(format, res)
// when: res = "asdf-qwer"
// arr2 should be equal to arr
Anyone have experience doing something like this? I'm thinking about using regular expressions (modify the original format string, and then pass it to Regex.Matches to get the array) and run it for each placeholder in the format string. Is this feasible or is there any other more efficient solution?
While the comments about lost information are valid, sometimes you just want to get the string values of of a string with known formatting.
One method is this blog post written by a friend of mine. He implemented an extension method called string[] ParseExact(), akin to DateTime.ParseExact(). Data is returned as an array of strings, but if you can live with that, it is terribly handy.
public static class StringExtensions
{
public static string[] ParseExact(
this string data,
string format)
{
return ParseExact(data, format, false);
}
public static string[] ParseExact(
this string data,
string format,
bool ignoreCase)
{
string[] values;
if (TryParseExact(data, format, out values, ignoreCase))
return values;
else
throw new ArgumentException("Format not compatible with value.");
}
public static bool TryExtract(
this string data,
string format,
out string[] values)
{
return TryParseExact(data, format, out values, false);
}
public static bool TryParseExact(
this string data,
string format,
out string[] values,
bool ignoreCase)
{
int tokenCount = 0;
format = Regex.Escape(format).Replace("\\{", "{");
for (tokenCount = 0; ; tokenCount++)
{
string token = string.Format("{{{0}}}", tokenCount);
if (!format.Contains(token)) break;
format = format.Replace(token,
string.Format("(?'group{0}'.*)", tokenCount));
}
RegexOptions options =
ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;
Match match = new Regex(format, options).Match(data);
if (tokenCount != (match.Groups.Count - 1))
{
values = new string[] { };
return false;
}
else
{
values = new string[tokenCount];
for (int index = 0; index < tokenCount; index++)
values[index] =
match.Groups[string.Format("group{0}", index)].Value;
return true;
}
}
}
You can't unformat because information is lost. String.Format is a "destructive" algorithm, which means you can't (always) go back.
Create a new class inheriting from string, where you add a member that keeps track of the "{0}-{1}" and the { "asdf", "qwer" }, override ToString(), and modify a little your code.
If it becomes too tricky, just create the same class, but not inheriting from string and modify a little more your code.
IMO, that's the best way to do this.
It's simply not possible in the generic case. Some information will be "lost" (string boundaries) in the Format method. Assume:
String.Format("{0}-{1}", "hello-world", "stack-overflow");
How would you "Unformat" it?
Assuming "-" is not in the original strings, can you not just use Split?
var arr2 = formattedString.Split('-');
Note that this only applies to the presented example with an assumption. Any reverse algorithm is dependent on the kind of formatting employed; an inverse operation may not even be possible, as noted by the other answers.
A simple solution might be to
replace all format tokens with (.*)
escape all other special charaters in format
make the regex match non-greedy
This would resolve the ambiguities to the shortest possible match.
(I'm not good at RegEx, so please correct me, folks :))
After formatting, you can put the resulting string and the array of objects into a dictionary with the string as key:
Dictionary<string,string []> unFormatLookup = new Dictionary<string,string []>
...
var arr = new string [] {"asdf", "qwer" };
var res = string.Format(format, arr);
unFormatLookup.Add(res,arr);
and in Unformat method, you can simply pass a string and look up that string and return the array used:
string [] Unformat(string res)
{
string [] arr;
unFormatLoopup.TryGetValue(res,out arr); //you can also check the return value of TryGetValue and throw an exception if the input string is not in.
return arr;
}