I saw a code snippet the other day that converts a Boolean value to the corresponding "Yes"/"No" value:
CDbl(True).ToString("Yes;Yes;No")
The code works fine but I'm curious how it works and I haven't been able to find the answer in the MSDN documentation for ToString().
Can anybody shed some light on this?
String Formatting The most common way how we format strings is by using string. Format() . In C#, the string Format method is used to insert the value of the variable or an object or expression into another string. By using the string.
%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string.
An algorithm is given below to explain the process which is included in the C programming language to print the characters and strings in different formats. Step 1: Read a character to print. Step 2: Read a name at compile time. Step 3: Output of characters in different formats by using format specifiers.
In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Parameter: The locale value to be applied on the format() method.
Take a look here and here, for official documentation. And this great cheatsheet from Jhon Sheehan's Blog!
It's treating it as a Custom Numeric Format String. Specifically, see the part about section separators in the linked page:
The ';' character is used to separate sections for positive, negative, and zero numbers in the format string. If there are two sections in the custom format string, the leftmost section defines the formatting of positive and zero numbers, while the rightmost section defines the formatting of negative numbers. If there are three sections, the leftmost section defines the formatting of positive numbers, the middle section defines the formatting of negative numbers, and the rightmost section defines the formatting of zero numbers.
It's using the literal format string from the customized numeric format strings. You can supply a literal that maps onto numbes that are postive, negative, or zero numbers. The first "yes" maps to positive, the second to negative, and the "no" to zeros. Thus any non-zero is yes, and only zeros are no. This is equivalent to standard true/false semantic interpretations on numeric values.
Look under "section separator" of the Custom Numeric Format strings page.
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