CodeGym/Java Blog/ランダム/JavaでintをStringに変換する方法
John Squirrels
レベル 41
San Francisco

JavaでintをStringに変換する方法

ランダム グループに公開済み
人のメンバー
この記事では、int (プリミティブ型) とオブジェクト型 (ラッパー) の整数から文字列への変換について説明します。Java でそれを行う方法はたくさんあります。

Javaでintを文字列に変換する方法

  1. 空の文字列を追加して変換します。

    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

  2. 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

    Integer.toString method toString(int i, int base)数値 i の文字列表現を基底と文字列に返すスペシャルを使用できます。例えば

    構文は次のとおりです。

    public static String toString(int i, int base)

    以下に例を示します。

    int a = 255;
    //  binary
    String customString = Integer.toString(a, 2);
    System.out.println(customString);

    出力は、10 進数 255 の文字列バイナリ表現です。

    11111111

  3. String.valueOf(int) を使用して int を String に変換します

    このメソッドは、String.valueOf(int)int 引数の文字列表現を返します。

    メソッドの構文は次のとおりです。

    public static String valueOf(int i)

    以下は、次を使用して int を String に変換する Java の例です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

  4. DecimalFormat を使用して変換する

    java.text.DecimalFormatは packageで定義されたクラスでありjava.text、 のサブクラスですNumberFormat。これは、10 進数を特定のパターンに従うことを表す文字列にフォーマットするために使用されます。整数にも使用できます。

    例:

    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

  5. String.format() を使用して変換する

    String.format() は、整数を文字列オブジェクトに変換するもう 1 つの方法です。

    構文

    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

コメント
  • 人気
  • 新規
  • 古い
コメントを残すには、サインインしている必要があります
このページにはまだコメントがありません