此方法用於返回指定的字符串實例,並且不執行任何實際轉換。
用法:
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
參考:
相關用法
- C# Boolean.ToString(IFormatProvider)用法及代碼示例
- C# Convert.ToInt32(String, IFormatProvider)用法及代碼示例
- C# Convert.ToUInt16(String, IFormatProvider)用法及代碼示例
- C# Convert.ToSByte(String, IFormatProvider)用法及代碼示例
- C# Convert.ToInt64(String, IFormatProvider)用法及代碼示例
- C# Convert.ToSingle(String, IFormatProvider)用法及代碼示例
- C# Convert.ToInt16(String, IFormatProvider)用法及代碼示例
- C# Convert.ToUInt32(String, IFormatProvider)用法及代碼示例
- C# Convert.ToDouble(String, IFormatProvider)用法及代碼示例
- C# Convert.ToDecimal(String, IFormatProvider)用法及代碼示例
- C# Convert.ToDateTime(String, IFormatProvider)用法及代碼示例
- C# Convert.ToUInt64(String, IFormatProvider)用法及代碼示例
- C# Convert.ToBoolean(String, IFormatProvider)用法及代碼示例
- C# Convert.ToByte(String, IFormatProvider)用法及代碼示例
- C# Convert.ToChar(String, IFormatProvider)用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Convert.ToString(String, IFormatProvider) Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。