C# 中将 int 转换为 String 定义为将数字格式化为单个字符串值。将 int 转换为 String 的目的是大多数默认情况下接受的值是 String 类型,在接收到所需的 String 后,我们可以再次转换为 int 类型。这样,我们就可以克服类型格式问题。将 int 转换为 String 实现类型安全。
可以通过多种方式进行转换。我们将介绍将 int 转换为 String 的方法。
整数到字符串的转换是类型转换或类型转换。这可以将非十进制数字转换为字符串值。
语法:
int number=100; String stringNumber=number.ToString();
Int32.ToString() 方法将非十进制值转换为等效的字符串字符。
语法:
int number=Int32.MaxValue; // creating and initializing the object of CultureInfo CultureInfo provider = new CultureInfo("fr-FR"); // declaring and intializing format string format = "D5"; // using the method string str = number.ToString(format, provider);
我们可以在 String 和 int 参数之间使用+(加)运算符,然后 C# 编译器会自动在内部进行类型转换。
语法:
int number =214; string output = "Converted number is" + number + " and now it is a string";
字符串生成器用于执行字符串的可变性。我们还可以将整数值附加到 StringBuilder 以将 int 转换为 String。
语法:
int num = 400; var stringBuilder = new StringBuilder(); stringBuilder.Append(num).ToString();
我们还可以使用 Convert.ToString() 方法将 int 转换为 string。
语法:
int num=500; string s=Convert.ToString(num);
我们还可以使用 string.Format() 方法将 int 转换为 string。
语法:
int num=800; string s=string.Format(num);
以下是下面提到的示例
int 到字符串转换
代码:
//including C# basic libraries using System; //creating class public class ToStringClass { public static void Main(string[] args) { //declaring String variable String strinToNumber; //declaring and initializing int variable int number = 500; //Converting int to string by using ToString() method strinToNumber = number.ToString(); //Display output Console.WriteLine("Converting int to String by using ToString() method result is = "+strinToNumber); } }
输出:
使用 Int32.ToString() 将 int 转换为字符串
代码:
//including C# basic libraries using System; //including CultureInfo class using System.Globalization; //creating class public class Int32ToStringClass { public static void Main(string[] args) { //Declaring and initializing int variable int number=Int32.MaxValue; // creating and initializing the object of CultureInfo CultureInfo provider = new CultureInfo("fr-FR"); // declaring and intializing format string format = "D5"; // Converting int to string by using Int32.ToString() method string str = number.ToString(format, provider); //Display the output Console.WriteLine("Converting int to String by using Int32.ToString() method result is = "+str); } }
输出:
通过字符串连接将 int 转换为 string
代码:
//including C# basic libraries using System; //creating class public class CancatClass { public static void Main(string[] args) { //Declaring and initializing int variable int number=555; //Display the output Console.WriteLine("Converting int to String by using cancat operation(+) result is = "+number+" and now it is becomes string"); } }
输出:
使用 StringBuilder 将 int 转换为字符串
代码:
//including C# basic libraries using System; //including StringBuilder class package using System.Text; //creating class public class StringBuilderClass { public static void Main(string[] args) { //Declaring and initializing int variable int number=707; //creating StringBuilder object var stringBuilder = new StringBuilder(); //Adding int value to Strigbuilder to make int as String stringBuilder.Append(number); //Display the output Console.WriteLine("Converting int to String by using StringBuilder class then result is = "+stringBuilder); } }
输出:
使用 Convert.ToString() 将 int 转换为字符串
代码:
//including C# basic libraries using System; //creating class public class CovertToStringClass { public static void Main(string[] args) { //Declaring and initializing int variable int number=989; //Converting int to string byy using Convert.ToString() method string output=Convert.ToString(number); //Display the output Console.WriteLine("Converting int to String by using Convert.ToString() method then the result is = "+output); } }
输出:
使用 string.Format() 将 int 转换为字符串
代码:
//including C# basic libraries using System; //creating class public class StringFormatClass { public static void Main(string[] args) { //Declaring and initializing int variable int number=214; //Converting int to string byy using Convert.ToString() method string outputString=string.Format("Converting int to String by using string.Format() method then the result is = "+number); //Display the output Console.WriteLine(outputString); } }
输出:
C#中int转string用于将非十进制数字转换为字符串字符。这可以通过使用 int 到字符串转换、使用 Int32.ToString() 将 int 转换为字符串、使用字符串连接将 int 转换为字符串、使用 StringBuilder 将 int 转换为字符串、使用 Convert.ToString() 将 int 转换为字符串以及使用字符串将 int 转换为字符串。格式()。
以上是将 int 转换为 String C#的详细内容。更多信息请关注PHP中文网其他相关文章!