⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 format.cs

📁 java基础方面的一些实例代码
💻 CS
字号:
using System;
using System.Globalization;
using System.Threading;

class StrFormat
{
	static void Main(string[] args)
	{

		Console.WriteLine("NumberFormat()");
		NumberFormat();
		Console.WriteLine("DateFormat()");
		DateFormat();
		Console.WriteLine("ViewExample()");
		ViewExample();
	}
	static void NumberFormat()
	{
		double MyDouble = 123; int MyInt = 100;
		string FormatStr = String.Format("MyInt = {0,10},MyDouble = {1,10:C02}", MyInt,MyDouble); 
		Console.WriteLine(FormatStr);
		FormatStr = String.Format("MyInt = {0,-10:X},MyDouble = {1,-10:C02}", MyInt,MyDouble); 
		Console.WriteLine(FormatStr);       
		
	}
	static void DateFormat()
	{
		DateTime dt = DateTime.Now;
		Console.WriteLine(dt);
		String[] format = {
							  "d", "D",
							  "f", "F",
							  "g", "G",
							  "m",
							  "r",
							  "s",
							  "t", "T",
							  "u", "U",
							  "y",
							  "dddd, MMMM dd yyyy",
							  "ddd, MMM d \"'\"yy",
							  "dddd, MMMM dd",
							  "M/yy",
							  "dd-MM-yy"
		};
		String date;
		for (int i = 0; i < format.Length; i++) 
		{
			date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
			Console.WriteLine(String.Concat(format[i], " :" , date));
		}

	}

	static void ViewExample()
	{
		string myFName = "Fred";
		string myLName = "Opals";
		int myInt = 100;

		DateTime dt = DateTime.Now;
		Console.WriteLine(dt);
		string FormatString1 = String.Format("{0:dddd MMMM}",dt);
		string FormatString2 = dt.ToString("dddd MMMM");
		Console.WriteLine(FormatString1);
		Console.WriteLine(FormatString2);
		string Fstr = String.Format("Name = {0}, hours = {1:hh}", myFName, DateTime.Now);
		Console.WriteLine(Fstr);

		string FormatFName = String.Format("First Name = {0,10}", myFName);
		string FormatLName = String.Format("Last Name = {0,10}", myLName);
		string FormatPrice = String.Format("Price = {0,10:C02}", myInt); 
		Console.WriteLine(FormatFName);
		Console.WriteLine(FormatLName);
		Console.WriteLine(FormatPrice);

		FormatFName = String.Format("First Name = |{0,-10}|", myFName);
		FormatLName = String.Format("Last Name = |{0,10}|", myLName);
		FormatPrice = String.Format("Price = |{0,10:C02}|", myInt);
		Console.WriteLine(FormatFName);
		Console.WriteLine(FormatLName);
		Console.WriteLine(FormatPrice);

		DateTimeFormatInfo myDTFI = new CultureInfo( "zh-CN", false ).DateTimeFormat;
		Console.WriteLine(myDTFI.FullDateTimePattern);
		Console.WriteLine(myDTFI.MonthDayPattern);
		Console.WriteLine(myDTFI.ShortDatePattern);
		Console.WriteLine(myDTFI.LongDatePattern);
		Console.WriteLine(myDTFI.LongTimePattern);
		Console.WriteLine(myDTFI.ShortTimePattern);
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -