logo

Java の int から String への変換 - さまざまな変換方法

変換する int を文字列に変換する は重要な型変換です。文字列に対しては多くの操作を実行できますが、整数に関しては制限があります。 String クラスには、手間のかからない操作を実行するのに役立つさまざまな組み込みメソッドのリストがあります。 

2 つの整数を連結する必要があると仮定すると、それは面倒なことになります。番号体系内で数学をプレイすることになる対応する番号体系を扱う必要があるため、この手順を実行する必要があります。しかし Java で整数を文字列に変換するには、作業を非常に簡単にするいくつかの組み込みメソッドとクラスがあります。 



ヒント: という概念はありますが、通常はプリミティブ クラスのデータ メンバーの型を変換します。 ラッパークラス Java での実際のプログラミングでは文字列を扱うためです。

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

変換中 整数から文字列へ Integer クラスの使用が含まれます toString() または String.valueOf() 直接変換の場合。 文字列.format() は、柔軟な書式設定オプションを提供するもう 1 つの方法です。使用する 文字列ビルダー または 文字列バッファ 整数値を文字列として追加することは、広範な文字列操作に効率的です。

整数から文字列への変換には、以下に示す特定の方法があります。



  1. の使用 toString() Integerクラスのメソッド
  2. の使用 値の() Stringクラスのメソッド
  3. 使用する 整数(int).toString() Integerクラスのメソッド
  4. 空の文字列との連結を使用します。

1. IntegerクラスのtoString()メソッドの利用

Integer クラスには静的メソッドがあります toString() 指定された int パラメータを表す String オブジェクトを返します。 引数は変換され、文字列インスタンスとして返されます。数値が負の場合、符号は保持されます。

例:

Java
// Java Program to Illustrate Integer to String Conversions // Using toString() Method of Integer Class class Geeks {  // 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);  } } 

出力
String str1 = 1234 String str2 = -1234 




2. StringクラスのvalueOf()メソッドの利用

Stringクラスには静的メソッドがあります 値の() 以下に示すように、これを使用して整数を文字列に変換できます。

例:

Java
// Java Program to Illustrate Integer to String Conversions // Using valueOf() Method of String class class Geeks {  // Main driver method  public static void main(String args[])  {  // Custom integer input  int c = 1234;  // Converting 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 


3. 使用方法 整数(int).toString() Integerクラスのメソッド

このメソッドでは、Integer クラスのインスタンスを使用して toString() メソッドを呼び出すため、上で提案したメソッド 1 とは異なります。 

例:

Java
// Java Program to Illustrate // Integer to String Conversions // Using toString() Method of // Integer Class // Importing required classes import java.util.*; // Main class class Geeks {  // Main driver method  public static void main(String args[])  {  // Custom input integer  int d = 1234;  // Converting integer to string  // using toString() method of Integer class  String str4 = new Integer(d).toString();  // Printing the integer value stored in above string  System.out.println('String str4 = ' + str4);  } } 

出力
String str4 = 1234 

説明: 変数がプリミティブ型 (int) の場合は、Integer.toString(int) または String.valueOf(int) を使用することをお勧めします。ただし、変数がすでに Integer (プリミティブ型 int のラッパー クラス) のインスタンスである場合は、上に示したように toString() メソッドを呼び出すだけの方がよいでしょう。 

注記: 変換が実行される前に Integer クラスのインスタンスが作成されるため、このメソッドは効率的ではありません。そして非推奨となり、削除としてマークされました。


4. 空の文字列との連結の使用

ここでは空の文字列を宣言し、「+」演算子を使用して結果を文字列として単純に保存します。これにより、これらの文字列を正常に追加して連結できるようになりました。 

例:

Java
// Java Program to Illustrate Integer to String Conversions // Using Concatenation with Empty String class Geeks {  // 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 concatenated strings  System.out.println('String str1 = ' + str1);  System.out.println('String str2 = ' + str2);  } } 

出力
String str1 = 1234 String str2 = -1234 


を文字列に変換する高度な方法

以下に示す特定の事前メソッドがあります。

  1. DecimalFormatクラスの使用
  2. StringBuffer クラスの使用
  3. StringBuilderクラスを使用する 
  4. 特殊基数とカスタム基数の使用

1. DecimalFormatクラスの使用

10 進形式 数値を文字列にフォーマットするクラスです。 

女優ラクル プリート シン

例:

Java
// Java Program to Illustrate // Integer to String Conversions // Using DecimalFormat Class // Importing required classes import java.text.DecimalFormat; // Main class class Geeks {  // 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);  } } 

出力
12345 

ヒント: この方法を使用すると、読みやすくするために小数点以下の桁数とカンマ区切り文字を指定できます。


2. StringBuffer クラスの使用   

文字列バッファ 複数の値を文字列に連結するために使用されるクラスです。 

例 1:

Java
// Java Program to Illustrate // Integer to String Conversions // Using StringBuffer Class // Main class class Geeks {  // 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 


例 2:

Java
// Java Program to Illustrate // Integer to String Conversions // Using StringBuffer Class class Geeks {  // 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 


3. StringBuilder クラスの使用

文字列ビルダー 同様に動作しますが、StringBuffer のようにスレッドセーフではありません。 

例 1:

Java
// Java Program to Illustrate // Integer to String Conversions // Using StringBuilder Class // Main class class Geeks {  // Main driver method  public static void main(String args[])  {  // Input integer  int g = 1234;  // Creating an object of StringBuilder class  // inside main() method  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 


例 2:

Java
// Java Program to Illustrate Different Ways for // Integer to String Conversions // Using StringBuilder Class // Main class class Geeks {  // 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 

注記: 上記のすべての例では、基数 (基数) 10 を使用しています。次に、2 進数の 8 進数および 16 進数のシステムに変換する便利な方法を示します。任意のカスタム番号システムもサポートされています。 


int をベースが異なる文字列に変換する

int を異なる基数の String に変換することもできます。以下では、int を異なる基数の String に変換するのに役立ついくつかの重要なメソッドについて説明します。

1. 特殊基数の使用 

1 : を使用して int (base 2 または 2 進数) を String に変換します。 整数クラス toBinary() メソッド。

Java
// Java Program to Illustrate Integer to String Conversions // Using Special Radix In Binary Numbers // Main class class Geeks {  // 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 の 2 進数表現です。


例 2: int(基数8または8進数)を文字列に変換する 整数クラス メソッドをOctalString()にします。

Java
// Java Program to Illustrate Integer to String Conversions // Using Special Radix In Octal Numbers // Main class class Geeks {  // 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 を 8 進数で表現したものです。 


例 3: を使用して int (基数 10 または 16 進数) を文字列に変換します 整数クラス メソッド toHexString()。

Java
// Java Program to Illustrate Integer to String Conversions // Using Special Radix In Hexadecimal Numbers // Main class class Geeks {  // 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 を 16 進数で表現したものです。

2. カスタム基数/基数 

アプローチ: Integer クラスの toString() メソッドを使用して文字列に変換し、さらに radix と呼ばれる引数として値を渡します。 int を文字列に変換するときに、他のカスタム基数/基数を使用できます。以下の例では、説明のために 7 進数の数値体系を考慮しています。 

例: を使用して int (base 7 またはカスタム基数) を String に変換します。 整数クラス メソッド toString(int I int radix)。

Java
// Java Program to Illustrate Integer to String Conversions // Using Custom Radix // Main class class Geeks {  // 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 

注記: 513 は、数値 255 を 7 進法で表したものです。