Csharp/C Sharp/Data Types/String Format

Материал из .Net Framework эксперт
Версия от 11:46, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

|{0,10:X}|{1,10}|{2:X}|{3}|

 
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);
    }
}


{4,4}

 
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]);
    }
}


Control the width

 
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);
    }
}


Fill placeholders using an array of objects.

 
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);
    }
}


Format a string

 
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);
    }
}


Format with {0:F}

 
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);
    }
}


left justify and align a set of strings to improve the appearance of program output

 
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;
    }
}


The comma (,M) determines the field width and justification.

 
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);
    }
}


Use string.Format to format integer

 
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);
    }
}


use the Format() method to format a string

 
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);
    
    }
}


Use the static String.Format() method to build a new string.

 
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);
    }
}