Csharp/C Sharp/Data Types/String Format — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:46, 26 мая 2010
Содержание
- 1 |{0,10:X}|{1,10}|{2:X}|{3}|
- 2 {4,4}
- 3 Control the width
- 4 Fill placeholders using an array of objects.
- 5 Format a string
- 6 Format with {0:F}
- 7 left justify and align a set of strings to improve the appearance of program output
- 8 The comma (,M) determines the field width and justification.
- 9 Use string.Format to format integer
- 10 use the Format() method to format a string
- 11 Use the static String.Format() method to build a new string.
|{0,10:X}|{1,10}|{2:X}|{3}|
<source lang="csharp"> public class MainClass {
public static void Main() { int i = 250662; string s = string.Format("|{0,10:X}|{1,10}|{2:X}|{3}|", i, i, i, i); }
}
</source>
{4,4}
<source lang="csharp"> using System; public class MainClass {
public static void Main() { Random rnd = new Random(); int[,] m = new int[3, 5]; for (int i = 0; i < m.GetLength(0); i++) for (int j = 0; j < m.GetLength(1); j++) m[i, j] = rnd.Next(1000); for (int i = 0; i < m.GetLength(0); i++) Console.WriteLine("{0,4} {1,4} {2,4} {3,4} {4,4}", m[i, 0], m[i, 1], m[i, 2], m[i, 3], m[i, 4]); }
}
</source>
Control the width
<source lang="csharp"> using System; public class TestConsoleApp {
public static void Main(string[] args) { Console.WriteLine("\n{0,-10}{1,-3}", "Name", "Salary"); Console.WriteLine("{0,-10}{1,6}", "Bill", 123456); Console.WriteLine("{0,-10}{1,6}", "Polly", 7890); }
}
</source>
Fill placeholders using an array of objects.
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Program {
static void Main(string[] args) { object[] stuff = { "Hello", 20.9, 1, "There", "83", 99.99933 }; Console.WriteLine("The Stuff: {0} , {1} , {2} , {3} , {4} , {5} ", stuff); Console.WriteLine("{0}, Number{0}, Number{0}", 9); }
}
</source>
Format a string
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Program {
static void Main(string[] args) { int theInt = 90; double theDouble = 9.99; bool theBool = true; Console.WriteLine("Int is: {0}\nDouble is: {1}\nBool is: {2}",theInt, theDouble, theBool); }
}
</source>
Format with {0:F}
<source lang="csharp"> using System; using System.Globalization; using System.Text; public class NumParsingApp {
public static void Main(string[] args) { string u = "AA -1,234,567.890 "; NumberFormatInfo ni = new NumberFormatInfo(); ni.CurrencySymbol = "AA"; double h = Double.Parse(u, NumberStyles.Any, ni); Console.WriteLine("h = {0:F}", h); }
}
</source>
left justify and align a set of strings to improve the appearance of program output
<source lang="csharp"> using System; class Class1 {
public static void Main(string[] args) { string[] names = {"CA "," SA","J A","SA"," SA "}; foreach (string s in names) { Console.WriteLine("This is the name "{0}" before", s); } string[] sAlignedNames = TrimAndPad(names); foreach (string s in sAlignedNames) { Console.WriteLine("This is the name "{0}" afterwards", s); } } public static string[] TrimAndPad(string[] strings) { string[] stringsToAlign = new String[strings.Length]; for (int i = 0; i < stringsToAlign.Length; i++) { stringsToAlign[i] = strings[i].Trim(); } int nMaxLength = 0; foreach (string s in stringsToAlign) { if (s.Length > nMaxLength) { nMaxLength = s.Length; } } for (int i = 0; i < stringsToAlign.Length; i++) { stringsToAlign[i] = stringsToAlign[i].PadRight(nMaxLength + 1); } return stringsToAlign; }
}
</source>
The comma (,M) determines the field width and justification.
<source lang="csharp"> using System; public class TestConsoleApp {
public static void Main(string[] args) { Console.WriteLine( "{0,5} {1,5}", 123, 456); // Right-aligned Console.WriteLine( "{0,-5} {1,-5}", 123, 456); // Left-aligned Console.WriteLine( "{0,-10:D6} {1,-10:D6}", 123, 456); }
}
</source>
Use string.Format to format integer
<source lang="csharp"> using System; public class TestConsoleApp {
public static void Main(string[] args) { string s = string.Format("123"); string t = string.Format("{0}", 123); string u = string.Format("{0:D3}", 123); Console.WriteLine(s); Console.WriteLine(t); Console.WriteLine(u); }
}
</source>
use the Format() method to format a string
<source lang="csharp"> using System; class MainClass {
public static void Main() { float myFloat = 1234.56789f; string myString8 = String.Format("{0, 10:f3}", myFloat); Console.WriteLine("String.Format(\"{0, 10:f3}\", myFloat) = " + myString8); }
}
</source>
Use the static String.Format() method to build a new string.
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Program {
static void Main(string[] args) { string formatStr; formatStr = String.Format("Don"t you wish you had {0:C} in your account?", 99989.987); Console.WriteLine(formatStr); }
}
</source>