当前位置: 首页>>代码示例>>C#>>正文


C# StringBuilder.AppendFormat方法代码示例

本文整理汇总了C#中System.Text.StringBuilder.AppendFormat方法的典型用法代码示例。如果您正苦于以下问题:C# StringBuilder.AppendFormat方法的具体用法?C# StringBuilder.AppendFormat怎么用?C# StringBuilder.AppendFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.StringBuilder的用法示例。


在下文中一共展示了StringBuilder.AppendFormat方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Globalization;
using System.Text;

public class Example
{
   public static void Main()
   {
      Random rnd = new Random();
      CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
      StringBuilder sb = new StringBuilder();
      string formatString = "    {0,12:N0} ({0,8:X8})\n" +
                            "And {1,12:N0} ({1,8:X8})\n" +
                            "  = {2,12:N0} ({2,8:X8})\n";
      for (int ctr = 0; ctr <= 2; ctr++) {
         int value1 = rnd.Next();
         int value2 = rnd.Next();
         sb.AppendFormat(culture, formatString,
                         value1, value2, value1 & value2).
                         AppendLine();
      }
      Console.WriteLine(sb.ToString());
   }
}
开发者ID:.NET开发者,项目名称:System.Text,代码行数:25,代码来源:StringBuilder.AppendFormat

输出:

1 984 112 195 (76432643)
And 1 179 778 511 (4651FDCF)
= 1 178 674 243 (46412443)

2 034 813 710 (7948CB0E)
And  569 333 976 (21EF58D8)
=  558 385 160 (21484808)

126 717 735 (078D8F27)
And 1 830 715 973 (6D1E8245)
=   84 705 797 (050C8205)

示例2: StringBuilder

//引入命名空间
using System;
using System.Text;
using System.Globalization;

class Sample 
{
    static StringBuilder sb = new StringBuilder();

    public static void Main() 
    {
    int    var1   = 111;
    float  var2   = 2.22F;
    string var3   = "abcd";
    object[] var4 = {3, 4.4, 'X'};

    Console.WriteLine();
    Console.WriteLine("StringBuilder.AppendFormat method:");
    sb.AppendFormat("1) {0}", var1);
    Show(sb);
    sb.AppendFormat("2) {0}, {1}", var1, var2);
    Show(sb);
    sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3);
    Show(sb);
    sb.AppendFormat("4) {0}, {1}, {2}", var4);
    Show(sb);
    CultureInfo ci = new CultureInfo("es-ES", true);
    sb.AppendFormat(ci, "5) {0}", var2);
    Show(sb);
    }

    public static void Show(StringBuilder sbs)
    {
    Console.WriteLine(sbs.ToString());
    sb.Length = 0;
    }
}
开发者ID:.NET开发者,项目名称:System.Text,代码行数:37,代码来源:StringBuilder.AppendFormat

输出:

StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22

示例3: Main

//引入命名空间
using System;
using System.Globalization;
using System.Text;

public class Example
{
   public static void Main()
   {
      StringBuilder sb = new StringBuilder();
      Decimal value = 16.95m;
      CultureInfo enGB = CultureInfo.CreateSpecificCulture("en-GB");
      DateTime dateToday = DateTime.Now;
      sb.AppendFormat(enGB, "Final Price: {0:C2}", value);
      sb.AppendLine();
      sb.AppendFormat(enGB, "Date and Time: {0:d} at {0:t}", dateToday);
      Console.WriteLine(sb.ToString());
   }
}
开发者ID:.NET开发者,项目名称:System.Text,代码行数:19,代码来源:StringBuilder.AppendFormat

输出:

Final Price: £16.95
Date and Time: 01/10/2014 at 10:22

示例4: Customer

//引入命名空间
using System;
using System.Text;

public class Customer
{
   private string custName;
   private int custNumber;
   
   public Customer(string name, int number)
   {
      this.custName = name;
      this.custNumber = number;
   }
   
   public string Name
   {
      get { return this.custName; }
   }
   
   public int CustomerNumber
   {
      get { return this.custNumber; }
   }
}

public class CustomerNumberFormatter : IFormatProvider, ICustomFormatter
{   
   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(ICustomFormatter))
         return this;
      return null;
   }
   
   public string Format(string format, object arg, IFormatProvider provider)
   {
      if (arg is Int32)
      {
         string custNumber = ((int) arg).ToString("D10");
         return custNumber.Substring(0, 4) + "-" + custNumber.Substring(4, 3) + 
                "-" + custNumber.Substring(7, 3);
      }
      else
      {
         return null;
      }
   }                   
}

public class Example
{
   public static void Main()
   {
      Customer customer = new Customer("A Plus Software", 903654);
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat(new CustomerNumberFormatter(), "{0}: {1}", 
                      customer.CustomerNumber, customer.Name);
      Console.WriteLine(sb.ToString());
   }
}
开发者ID:.NET开发者,项目名称:System.Text,代码行数:61,代码来源:StringBuilder.AppendFormat

输出:

0000-903-654: A Plus Software

示例5: Main

//引入命名空间
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

public class Example
{
   public static void Main()
   {
      CultureInfo culture = new CultureInfo("en-US");
      StringBuilder sb = new StringBuilder();
      Dictionary<DateTime, Double> temperatureInfo = new Dictionary<DateTime, Double>(); 
      temperatureInfo.Add(new DateTime(2010, 6, 1, 14, 0, 0), 87.46);
      temperatureInfo.Add(new DateTime(2010, 12, 1, 10, 0, 0), 36.81);

      sb.AppendLine("Temperature Information:\n");
      foreach (var item in temperatureInfo)
      {
         sb.AppendFormat(culture,
                         "Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F\n",
                         item.Key, item.Value);
      }
      Console.WriteLine(sb.ToString());
   }
}
开发者ID:.NET开发者,项目名称:System.Text,代码行数:26,代码来源:StringBuilder.AppendFormat

输出:

Temperature Information:

Temperature at  2:00 PM on  6/1/2010:  87.5°F
Temperature at 10:00 AM on 12/1/2010:  36.8°F

示例6: Main

//引入命名空间
using System;
using System.Text;

class StringApp {
    static void Main(string[] args) {
        StringBuilder myBuffer = new StringBuilder("My string data");
        Console.WriteLine(myBuffer.Capacity);
        myBuffer.Append(" contains some numerical data: ");
        myBuffer.AppendFormat("{0}, {1}.", 44, 99);
        Console.WriteLine(myBuffer.Capacity);
        Console.WriteLine(myBuffer);

    }
}
开发者ID:C#程序员,项目名称:System.Text,代码行数:15,代码来源:StringBuilder.AppendFormat


注:本文中的System.Text.StringBuilder.AppendFormat方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。