Click here to Skip to main content
15,891,981 members
Articles / Programming Languages / C#

Inside C#, Second Edition: String Handling and Regular Expressions Part 1

Rate me:
Please Sign up or sign in to vote.
4.94/5 (69 votes)
14 May 200217 min read 725.4K   2.2K   232  
This article will examine the String class, some of its simple methods, and its range of formatting specifiers.
using System;

namespace FormatSpec
{
	public class FormatSpecApp
	{
		public static void Main(string[] args)
		{
			int i = 123456;
			Console.WriteLine("{0:C}", i);	// �123,456.00
			Console.WriteLine("{0:D}", i);	// 123456
			Console.WriteLine("{0:E}", i);	// 1.234560E+005
			Console.WriteLine("{0:F}", i);	// 123456.00
			Console.WriteLine("{0:G}", i);	// 123456
			Console.WriteLine("{0:N}", i);	// 123,456.00
			Console.WriteLine("{0:P}", i);	// 12,345,600.00 %
			Console.WriteLine("{0:X}", i);	// 1E240

			Console.WriteLine();
			Console.WriteLine("{0:C5}", i);	// �123,456.00000
			Console.WriteLine("{0:D5}", i);	// 123456
			Console.WriteLine("{0:E5}", i);	// 1.23456E+005
			Console.WriteLine("{0:F5}", i);	// 123456.00000
			Console.WriteLine("{0:G5}", i);	// 1.23456E5
			Console.WriteLine("{0:N5}", i);	// 123,456.00000
			Console.WriteLine("{0:P5}", i);	// 12,345,600.00000 %
			Console.WriteLine("{0:X5}", i);	// 1E240

			Console.WriteLine();
			double d = 1.2345678901234567890;
			Console.WriteLine(
				"Floating-Point:\t{0:F16}", d);	// 1.2345678901234600
			Console.WriteLine(
				"Roundtrip:\t{0:R16}", d);		// 1.2345678901234567

			Console.WriteLine();
			Console.WriteLine("{0:#0}", i);				// 123456
			Console.WriteLine("{0:#0;(#0)}", i);		// 123456
			Console.WriteLine("{0:#0;(#0);<zero>}", i);	// 123456
			Console.WriteLine("{0:#%}", i);				// 12345600%

			i = -123456;
			Console.WriteLine();
			Console.WriteLine("{0:#0}", i);				// -123456
			Console.WriteLine("{0:#0;(#0)}", i);		// (123456)
			Console.WriteLine("{0:#0;(#0);<zero>}", i);	// (123456)
			Console.WriteLine("{0:#%}", i);				// -12345600%

			i = 0;
			Console.WriteLine();
			Console.WriteLine("{0:#0}", i);				// 0
			Console.WriteLine("{0:#0;(#0)}", i);		// 0
			Console.WriteLine("{0:#0;(#0);<zero>}", i);	// <zero>
			Console.WriteLine("{0:#%}", i);				// %
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Microsoft
United States United States
I manage the strategy for the Azure Management Experience documentation at Microsoft. Those technologies include Open-Source DevOps (Terraform, Ansible, Jenkins), Azure PowerShell, and Azure CLI.

Comments and Discussions