當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Convert.ToString(String, IFormatProvider)用法及代碼示例


此方法用於返回指定的字符串實例,並且不執行任何實際轉換。

用法:

public static string ToString (String, IFormatProvider);

參數:


  • value:這是要返回的字符串。
  • provider:它是提供特定於區域性格式信息的對象。該參數被忽略。

返回值:此方法返回的值保持不變。

以下示例程序旨在說明Convert.ToString(String,IFormatProvider)方法的用法:

範例1:

// C# program to demonstrate the 
// Convert.ToString() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // creating object of CultureInfo 
            CultureInfo cultures =  
              new CultureInfo("en-US"); 
  
            // declaring and intializing String array 
            string[] values = {"amar", "akbar", 
                                    "anthony"}; 
  
            // calling get() Method 
            Console.Write("Converted string value"
                  + " from a specified string:"); 
  
            for (int j = 0; j < values.Length; j++)  
            { 
                get(values[j], cultures); 
            } 
        } 
  
        catch (FormatException e)  
        { 
            Console.WriteLine("\n"); 
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
  
        catch (OverflowException e)  
        { 
            Console.WriteLine("\n"); 
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
  
    // Defining get() method 
    public static void get(string s, 
               CultureInfo cultures) 
    { 
  
        // converting string to specified string 
        string val = Convert.ToString(s, cultures); 
  
        // display the converted string value 
        Console.Write(" {0} ", val); 
    } 
}
輸出:
Converted string value from a specified string: amar  akbar  anthony

範例2:

// C# program to demonstrate the 
// Convert.ToString() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // creating object of CultureInfo 
        CultureInfo cultures =  
          new CultureInfo("en-US"); 
  
        // declaring and initializing a String array 
        string value = "The Accidental Prime Minister"; 
  
        // calling get() Method 
        Console.Write("Converted String:"); 
  
        // converting string to specified string 
        string val = Convert.ToString(value, cultures); 
  
        // display the converted string value 
        Console.Write("{0}", val); 
    } 
}
輸出:
Converted String:The Accidental Prime Minister

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Convert.ToString(String, IFormatProvider) Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。