Home  >  Article  >  Backend Development  >  Convert int to String C#

Convert int to String C#

WBOY
WBOYOriginal
2024-09-03 15:17:13458browse

Converting int to String in C# defined as formatting numbers into a single string value. Purpose of this converting int to Strings is most of them by default accepting value is a String type and after receiving required String then we can convert into int type again. By this, we can overcome type format problems. Converting int to String achieves type safety.

How to Convert int to String in C#?

Converting can be done in many ways. We will come of the ways to convert int to String.

  • int to string conversion
  • int to string with Int32.ToString()
  • int to string with string concatenation
  • int to string with StringBuilder
  • int to string with Convert.ToString()
  • int to string with string.Format()

1. int to string conversion

Integer to String conversion is the type of typecasting or type conversion. This can convert non-decimal numbers to the string value.

Syntax:

int number=100;
String stringNumber=number.ToString();

2. int to string with Int32.ToString()

The Int32.ToString() method converts the non-decimal values into equivalent string characters.

Syntax:

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

3. int to string with string concatenation

We can use the +(plus) operator in between String and int arguments, then the C# compiler automatically performs type conversion internally.

Syntax:

int number =214;
string output = "Converted number is" + number + " and now it is a string";

4. int to string with StringBuilder

String builder is used to performing mutability of the strings. We can also append integer values to StringBuilder to convert int to String.

Syntax:

int num = 400;
var stringBuilder = new StringBuilder();
stringBuilder.Append(num).ToString();

5. int to string with Convert.ToString()

We can also use Convert.ToString() method to convert int to string.

Syntax:

int num=500;
string s=Convert.ToString(num);

6. int to string with string.Format()

We can also use string.Format() method to convert int to string.

Syntax:

int num=800;
string s=string.Format(num);

Examples of Convert int to String C#

Here are the following examples mention below

Example #1

int to string conversion

Code:

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

Output:

Convert int to String C#

Example #2

int to string with Int32.ToString()

Code:

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

Output:

Convert int to String C#

Example #3

int to string with string concatenation

Code:

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

Output:

Convert int to String C#

Example #4

int to string with StringBuilder

Code:

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

Output:

Convert int to String C#

Example #5

int to string with Convert.ToString()

Code:

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

Output:

Convert int to String C#

Example #6

int to string with string.Format()

Code:

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

Output:

Convert int to String C#

Conclusion

Converting int to string in C# is used to convert non-decimal numbers to string character. This can be done by using int to string conversion, int to string with Int32.ToString(), int to string with string concatenation, int to string with StringBuilder, int to string with Convert.ToString() and int to string with string.Format().

The above is the detailed content of Convert int to String C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn