Java中整数到字符串转换的不同方法
为了在Java中将整数转换为字符串,我们有一些内置的方法和类。
整数到字符串转换的不同方法
- 使用 Integer 类的 toString() 方法
- 使用 String 类的 valueOf() 方法
- 使用 Integer 类的 Integer(int).toString() 方法
- 使用 DecimalFormat 类
- 使用 StringBuffer 类
- 使用 StringBuilder 类
- 使用特殊基数和自定义基数
- 使用空字符串连接
方法一:使用 Integer 类的 toString 方法
Integer 类有一个静态方法,该方法返回一个表示指定 int 参数的 String 对象。参数被转换并作为字符串实例返回。如果数字为负数,则将保留符号。
例子:
Java
// Java Program to Illustrate
// Integer to String Conversions
// Using toString() Method of
// Integer Class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input integers
int a = 1234;
int b = -1234;
// Converting integer to string
// using toString() method
String str1 = Integer.toString(a);
String str2 = Integer.toString(b);
// Printing the above strings that
// holds integer
System.out.println("String str1 = " + str1);
System.out.println("String str2 = " + str2);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using valueOf() Method of
// String class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom integer input
int c = 1234;
// Coverting above integer to string
// using valueOf() Method
String str3 = String.valueOf(c);
// Printing the integer stored in above string
System.out.println("String str3 = " + str3);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using valueOf() Method of
// String class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Passing input integer as in argument and
// storing it in a string
String str3 = String.valueOf(1234);
// Printing the integer stored in above string
System.out.println("String str3 = " + str3);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Integer(int).toString() Method
// Importing required classes
import java.util.*;
// Main class
class GFG
{
// Main driver method
public static void main(String args[])
{
// Custom input integer
int d = 1234;
// Creating an object of Integer class
// inside main() method
Integer obj = new Integer(d);
String str4 = obj.toString();
// Printing the above string
// holding integer value
System.out.println("String str4 = " + str4);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Integer(int).toString() Method
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input integer
int d = 1234;
// Converting integer to string
// using toStirng() method of Integer class
String str4 = new Integer(d).toString();
// Printing the integer value stored in above string
System.out.println("String str4 = " + str4);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Integer(int).toString() Method
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Passing integer inside object as soon it is
// created via parametrised constructor and
// converting integer to string using toString()
// method and storing it into a String
String str4 = new Integer(1234).toString();
// Printing the above string holding integer value
System.out.println("String str4 = " + str4);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using DecimalFormat Class
// Importing requried classes
import java.text.DecimalFormat;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom integer input
int e = 12345;
// Creating an object of DecimalFormat class
// inside main() method
DecimalFormat df = new DecimalFormat("#");
// Formatting the integer to string
// and storing it in a string
String str5 = df.format(e);
// Printing the above stored value
// inside a string
System.out.println(str5);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using DecimalFormat Class
// Importing requried classes
import java.text.DecimalFormat;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Input integer value
int e = 12345;
// Creating an object of DecimalFormat class
// inside main() method
DecimalFormat df = new DecimalFormat("#,###");
// Converting above integral value to string
String Str5 = df.format(e);
// Printing the value stored in above string
System.out.println(Str5);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using StringBuffer Class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Integer input value
int f = 1234;
// Creating an object of StringBuffer class
StringBuffer sb = new StringBuffer();
sb.append(f);
String str6 = sb.toString();
System.out.println("String str6 = " + str6);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using StringBuffer Class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
String str6
= new StringBuffer().append(1234).toString();
System.out.println("String str6 = " + str6);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using StringBuilder Class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Input integer
int g = 1234;
// Creating an object of StringBuilder class inside
// main()
StringBuilder sb = new StringBuilder();
sb.append(g);
String str7 = sb.toString();
// Printing the value stored in above string
System.out.println("String str7 = " + str7);
}
}
Java
// Java Program to Illustrate Different Ways for
// Integer to String Conversions
// Using StringBuilder Class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
String str7
= new StringBuilder().append(1234).toString();
// Printing the value stored in above string
System.out.println("String str7 = " + str7);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Special Radix In Binary Numbers
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Input integer
int h = 255;
String binaryString = Integer.toBinaryString(h);
// Printing the binary number stored in above string
System.out.println(binaryString);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Special Radix In Octal Numbers
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input integer
int i = 255;
String octalString = Integer.toOctalString(i);
// Printing the octal number stored in above string
System.out.println(octalString);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Special Radix In Hexadecimal Numbers
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input integer
int j = 255;
String hexString = Integer.toHexString(j);
// Printing the hexadecimal number
// stored in above string
System.out.println(hexString);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Custom Radix
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Input integer value
int k = 255;
// Setting base as 7, converting integer to string
// using toString() method and
// storing it into a string
String customString = Integer.toString(k, 7);
// Printing value stored in above string
System.out.println(customString);
}
}
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Concatenation with Empty String
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom integer values
int a = 1234;
int b = -1234;
// Concatenating with empty strings
String str1 = "" + a;
String str2 = "" + b;
// Printing the concatinated strings
System.out.println("String str1 = " + str1);
System.out.println("String str2 = " + str2);
}
}
String str1 = 1234
String str2 = -1234
方法二:使用 String.valueOf(int) 方法
示例 A:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using valueOf() Method of
// String class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom integer input
int c = 1234;
// Coverting above integer to string
// using valueOf() Method
String str3 = String.valueOf(c);
// Printing the integer stored in above string
System.out.println("String str3 = " + str3);
}
}
String str3 = 1234
示例 B:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using valueOf() Method of
// String class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Passing input integer as in argument and
// storing it in a string
String str3 = String.valueOf(1234);
// Printing the integer stored in above string
System.out.println("String str3 = " + str3);
}
}
String str3 = 1234
方法三:使用 Integer 类的 Integer(int).toString() 方法
它与上面提出的方法 1 不同,因为在此方法中,我们使用 Integer 类的实例来调用其 toString() 方法。
示例 A:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Integer(int).toString() Method
// Importing required classes
import java.util.*;
// Main class
class GFG
{
// Main driver method
public static void main(String args[])
{
// Custom input integer
int d = 1234;
// Creating an object of Integer class
// inside main() method
Integer obj = new Integer(d);
String str4 = obj.toString();
// Printing the above string
// holding integer value
System.out.println("String str4 = " + str4);
}
}
输出:
示例 B:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Integer(int).toString() Method
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input integer
int d = 1234;
// Converting integer to string
// using toStirng() method of Integer class
String str4 = new Integer(d).toString();
// Printing the integer value stored in above string
System.out.println("String str4 = " + str4);
}
}
输出:
示例 C:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Integer(int).toString() Method
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Passing integer inside object as soon it is
// created via parametrised constructor and
// converting integer to string using toString()
// method and storing it into a String
String str4 = new Integer(1234).toString();
// Printing the above string holding integer value
System.out.println("String str4 = " + str4);
}
}
输出:
输出说明:如果变量是原始类型(int),最好使用Integer.toString(int)或String.valueOf(int)。但是如果变量已经是 Integer 的实例(原始类型 int 的包装类),最好只调用它的 toString() 方法,如上所示。
Note: This method is not efficient as an instance of the Integer class is created before conversion is performed.
方法四:使用 DecimalFormat 类
DecimalFormat是一个将数字格式化为字符串的类。
示例 A:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using DecimalFormat Class
// Importing requried classes
import java.text.DecimalFormat;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom integer input
int e = 12345;
// Creating an object of DecimalFormat class
// inside main() method
DecimalFormat df = new DecimalFormat("#");
// Formatting the integer to string
// and storing it in a string
String str5 = df.format(e);
// Printing the above stored value
// inside a string
System.out.println(str5);
}
}
12345
示例 B:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using DecimalFormat Class
// Importing requried classes
import java.text.DecimalFormat;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Input integer value
int e = 12345;
// Creating an object of DecimalFormat class
// inside main() method
DecimalFormat df = new DecimalFormat("#,###");
// Converting above integral value to string
String Str5 = df.format(e);
// Printing the value stored in above string
System.out.println(Str5);
}
}
输出:
Tip: Using this method, you can specify the number of decimal places and comma separator for readability.
方法五:使用 StringBuffer 类
StringBuffer 是一个用于将多个值连接成一个字符串的类。
示例 A:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using StringBuffer Class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Integer input value
int f = 1234;
// Creating an object of StringBuffer class
StringBuffer sb = new StringBuffer();
sb.append(f);
String str6 = sb.toString();
System.out.println("String str6 = " + str6);
}
}
String str6 = 1234
示例 B:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using StringBuffer Class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
String str6
= new StringBuffer().append(1234).toString();
System.out.println("String str6 = " + str6);
}
}
输出:
String str6 = 1234
方法六:使用 StringBuilder 类
StringBuilder 的工作方式类似,但不像 StringBuffer 那样是线程安全的。
示例 A:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using StringBuilder Class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Input integer
int g = 1234;
// Creating an object of StringBuilder class inside
// main()
StringBuilder sb = new StringBuilder();
sb.append(g);
String str7 = sb.toString();
// Printing the value stored in above string
System.out.println("String str7 = " + str7);
}
}
String str7 = 1234
示例 B:
Java
// Java Program to Illustrate Different Ways for
// Integer to String Conversions
// Using StringBuilder Class
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
String str7
= new StringBuilder().append(1234).toString();
// Printing the value stored in above string
System.out.println("String str7 = " + str7);
}
}
String str7 = 1234
Note: All the examples above use the base (radix) 10. Following are convenient methods to convert to binary, octal, and hexadecimal systems. The arbitrary custom number system is also supported.
方法 7-A:使用特殊基数
示例:二进制
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Special Radix In Binary Numbers
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Input integer
int h = 255;
String binaryString = Integer.toBinaryString(h);
// Printing the binary number stored in above string
System.out.println(binaryString);
}
}
11111111
输出说明: 11111111 是数字 255 的二进制表示。
示例:八进制
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Special Radix In Octal Numbers
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input integer
int i = 255;
String octalString = Integer.toOctalString(i);
// Printing the octal number stored in above string
System.out.println(octalString);
}
}
377
377 是数字 255 的八进制表示。
示例 3:十六进制
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Special Radix In Hexadecimal Numbers
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input integer
int j = 255;
String hexString = Integer.toHexString(j);
// Printing the hexadecimal number
// stored in above string
System.out.println(hexString);
}
}
ff
ff是数字 255 的十六进制表示。
方式 7-B:自定义基数/基数
方法:我们使用 Integer 类的 toString() 方法将其转换为字符串,另外我们将传递一个值作为称为基数的参数。将 int 转换为字符串时,可以使用任何其他自定义基数/基数。在下面的示例中,出于说明目的,我们正在考虑以 7 为基数的数字系统。
例子:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Custom Radix
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Input integer value
int k = 255;
// Setting base as 7, converting integer to string
// using toString() method and
// storing it into a string
String customString = Integer.toString(k, 7);
// Printing value stored in above string
System.out.println(customString);
}
}
输出:
513 is the representation of the number 255 when written in the base 7 system.
方法 8:使用空字符串连接
方法:在这里我们将声明一个空字符串并使用“+”运算符,我们将简单地将结果存储为字符串。现在凭借这一点,我们能够成功地附加和连接这些字符串。
例子:
Java
// Java Program to Illustrate the
// Integer to String Conversions
// Using Concatenation with Empty String
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom integer values
int a = 1234;
int b = -1234;
// Concatenating with empty strings
String str1 = "" + a;
String str2 = "" + b;
// Printing the concatinated strings
System.out.println("String str1 = " + str1);
System.out.println("String str2 = " + str2);
}
}
String str1 = 1234
String str2 = -1234