This is a quick tutorial on how you can convert from int data type to string data type in C++, Java, Python, PHP, JavaScript, Swift, and C#.
Table of Contents
Int To String Conversion In Different Programming Languages
Integer to String Conversion is one of the most basic aspects of programming that you might have ever needed at least once in your programming lifetime. As it is a hot topic and is supported by most of the popular programming languages, so I thought of writing an article about it. Int to String conversion is explained below with the help of examples in the different programming languages.
In each of these programming languages, there can be more than one way for int to string conversion but we’ve tried to find out the simplest. So, I listed one easy way for each of these languages.
Python
Converting an integer to a String is pretty simple in python and all you need to do is to use the inbuilt str() function. The code is written below.
1 2 3 4 5 |
number = 565 print(type(number)) #Converting Int To String numberAsString = str(565) print(type(numberAsString)) |
Output.
C++
In C++, to_string() function can be used to convert from an integer data type to a string data type as demonstrated in the following program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<iostream> #include<string> //To Use to_string() function #include <typeinfo> //To Get Variable Type Information using namespace std; int number = 493; string numberAsString = to_string(number); int main(){ cout << typeid(number).name() << endl; cout << typeid(numberAsString).name() << endl; return 0; } |
Output.
Java
Like to_string() function in C++, you can use the method toString() in Java for int to string conversion. The following java program do the conversion and checks if the int variable is being converted to a string.
1 2 3 4 5 6 7 8 9 10 11 |
class Main { public static void main(String args[]) { int number = 123; String numberAsString = Integer.toString(number); if (numberAsString instanceof String) { System.out.println("numberAsString is String"); } } } |
Output.
PHP
In PHP, strval() function can be used to convert any scaler value data type like an integer to a string variable. The example code for the same is written below.
1 2 3 4 5 6 |
<?php $number = 95; $numberAsString = strval($number); var_dump($number); var_dump($numberAsString); ?> |
Output.
JavaScript
Almost similar to Java, you can convert an integer value to a String value using the toString() method in JavaScript as well.
1 2 3 4 5 6 7 |
<script> number = 350; numberAsString = number.toString(); document.write(typeof number); document.write("<br>"); document.write(typeof numberAsString); </script> |
Output.
Swift
In swift, you can use the String() function to convert an integer to string variable.
1 2 3 4 |
let number = 432 let numberAsString = String(number) print(type(of: number)) print(type(of: numberAsString)) |
Output.
C#
Again in C# also, there’s an inbuilt function ToString() that can be used for the purpose of conversion of an integer variable to string.
1 2 3 4 5 6 7 8 9 10 |
using System; class HelloWorld { static void Main() { int number = 55; string numberAsString = number.ToString(); Console.Write(number.GetType().Name); Console.Write("\n"); Console.Write(numberAsString.GetType().Name); } } |
Output.
I hope that this tutorial helped you. If so, do share it with other people who might find it useful as well. As I also mentioned earlier, I just mentioned one of the ways for the integer to string conversion in different programming languages. If you know, do mention the other ways in the comments section.
Feel free to ask any questions related to this article in the comments section as well and don’t forget to subscribe to WTMatter for more such useful content.