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