Is it possible to add some descriptive text to a string format specifier?
Example:
string.Format ("{0:ForeName} is not at home", person.ForeName);
In the example ForeName
is added as description.
The above syntax is obviously incorrect, but just to show the idea.
The reason I am asking, is because in my case the strings are in a resource file, so in the resource file you currently only see
{0} is not at home
in some cases it is hard to grasp what the context of {0}
is.
EDIT:
In c# 6 string interpolation with the $
operator has been introduced, so string.Format
is not needed anymore:
$"{person.ForeName} is not at home";
String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text.
JavaScript's String type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values (UTF-16 code units). Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on.
We usually put comments into our resources file e.g. {0} = Forename
.
Then anybody who might be translating the string knows what {0}
represents and can translate accordingly.
Also if you use ReSharper, you can enter the comment at the same time when you are adding your string to resources.
Phil Haack and Peli have written a couple of interesting blog posts about alternatives to the default string.format function. They might interest you.
Basically they allow you to use object properties inside the format string like this:
string s = NamedFormat("Hello {FullName} ({EmailAdrress})!", person);
You can the related blog posts here:
Perhaps one of the solutions covered in those blog posts would suit your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With