Java中如何将int转换为string
-
通过添加空字符串进行转换。
将 int 转换为 String 的最简单方法非常简单。只需将空字符串“”添加到 int 或 Integer 中,您就会将 int 作为字符串获得。发生这种情况是因为添加 int 和 String 会得到一个新的 String。这意味着如果你有
int x = 5
,只需定义x + ""
,你就会得到你的新字符串。这是一个例子:
// converting int to string, Java public class Demo { public static void main(String[] args) { int x = 5; // java int to string String xText = x + ""; // the result output System.out.println("convert int to String, Java: " + xText); // the int output System.out.println("our int: " + x); // adding int and String gives the new String System.out.println("adding int = 5 and String = \"5\". The result is a new String = " + xText + x); // integer to string, Java code Integer y = 7; String yText = y + ""; System.out.println("convert Integer to String: " + yText); System.out.println("our Integer: " + y); System.out.println("adding Integer = 7 and String = \"7\". The result is a new String = " + y + yText); } }
输出是:
convert int to String, Java: 5 our int: 5 adding int = 5 and String = "5". The result is a new String = 55 convert Integer to String: 7 our Integer: 7 adding Integer = 7 and String = "7". The result is a new String = 77
-
Java 使用 Integer.toString(int) 将 int 转换为字符串
对象类是 Java 中的根类。这意味着每个 Java 类都直接或间接地继承自 Object 类,并且所有 Object 类方法都可用于所有 Java 类。
Object 有一个特殊的方法 toString() 可以将任何对象表示为字符串。所以,每个 Java 类也都继承了这个方法。但是,最好在您自己的类中覆盖此方法以获得适当的结果。
Integer 类的 toString() 方法返回表示指定 int 或 Integer 参数的 String 对象。
它的语法:
public static String toString(int i)
该方法转换参数 i 并将其作为字符串实例返回。如果数字是负数,符号将被保留。
例子:
// java integer to string using toString method public class Demo { public static void main(String[] args) { int x = -5; // java convert int to string using Integer.toString String xText = Integer.toString(x); // the result output System.out.println("convert int to String: " + xText); // the int output System.out.println("our int: " + x); // adding int and String gives the new String System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + xText + Integer.toString(x)); } }
convert int to String: -5 our int: -5 converting int = -5 and adding to String = "-5". The result is a new String = -5-5
您也可以使用 toString 方法来转换 Integer(包装类型)。
Integer number = -7; String numberAsString = Integer.toString(number); System.out.println("convert Integer to String: " + numberAsString);
结果是:
将整数转换为字符串:-7您可以使用 special
Integer.toString method toString(int i, int base)
,它返回数字 i 的字符串表示形式,其基数为 base,而不是 String。例如语法是:
public static String toString(int i, int base)
这是一个例子:
int a = 255; // binary String customString = Integer.toString(a, 2); System.out.println(customString);
输出是十进制数 255 的字符串二进制表示形式:
11111111
-
使用 String.valueOf(int) 将 int 转换为 String
方法
String.valueOf(int)
返回 int 参数的字符串表示形式。该方法的语法是:
public static String valueOf(int i)
下面是一个使用 Java 将 int 转换为 String 的示例
String.valueOf(int)
:public class Demo { public static void main(String[] args) { int z = -5; // Java int to String converting String zText = String.valueOf(z); // the result output System.out.println("convert int to String: " + zText); // the int output System.out.println("our int: " + z); // adding int and String gives the new String System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + zText + z); } }
convert int to String: -5 our int: -5 converting int = -5 and adding to String = "-5". The result is a new String = -5-5
您可以对 Integer(int 的包装类型)执行相同的操作:
Integer number = -7; String numberAsString = String.valueOf(number); System.out.println("convert Integer to String: " + numberAsString);
输出将是:
将整数转换为字符串:-7 -
使用 DecimalFormat 转换
java.text.DecimalFormat
是包中定义的类java.text
,是NumberFormat
. 它用于将十进制数格式化为表示遵循特定模式的字符串。我们也可以将它用于整数。例子:
import java.text.DecimalFormat; public class Demo { public static void main(String[] args) { int myNumber = 31415; DecimalFormat decimalFormat = new DecimalFormat("#"); String myNumberAsString = decimalFormat.format(myNumber); System.out.println(myNumberAsString); } }
输出是:
31415
-
使用 String.format() 转换
String.format()是另一种将整数转换为字符串对象的方法。
句法
public static String format(String format, Object... args)
例子
public class Demo { public static void main(String[] args) { int myNumber = 35; String myNumberAsString = String.format("%d", myNumber); // %d converter defines a single decimal integer variable. System.out.println(myNumberAsString); } }
输出是:
35
GO TO FULL VERSION