Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net ToString() format documentation

Tags:

.net

vb.net

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?

like image 658
Seth Reno Avatar asked Feb 06 '09 16:02

Seth Reno


People also ask

How do I format a string in C#?

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.

What is %s in string format?

%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string.

How strings are displayed with different formats?

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.

How do you write in string format?

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.


3 Answers

Take a look here and here, for official documentation. And this great cheatsheet from Jhon Sheehan's Blog!

like image 97
tanathos Avatar answered Sep 25 '22 09:09

tanathos


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.

like image 28
Joel Coehoorn Avatar answered Sep 21 '22 09:09

Joel Coehoorn


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.

like image 39
tvanfosson Avatar answered Sep 23 '22 09:09

tvanfosson