変換中 整数から文字列へ Integer クラスの使用が含まれます toString() または String.valueOf() 直接変換の場合。 文字列.format() は、柔軟な書式設定オプションを提供するもう 1 つの方法です。使用する 文字列ビルダー または 文字列バッファ 整数値を文字列として追加することは、広範な文字列操作に効率的です。
多くの演算は文字列に対して実行できますが、整数に関しては制限されているため、私たちは通常、このような変換記事で反対します。 String クラスには、手間のかからない操作を実行するのに役立つさまざまな組み込みメソッドのリストがあります。
2 つの整数を連結する必要があるとします。その場合、数値体系内で数学を行うことになる対応する数値体系を処理する必要があるため、面倒な作業になります。しかし するために Java で整数を文字列に変換するには、作業を非常に簡単にする組み込みのメソッドとクラスがいくつかあります。
ヒント: Java での実際のプログラミングでは文字列を扱うため、ラッパー クラスの概念を文字列に変換しますが、通常はプリミティブ クラスのデータ メンバーの型を変換します。
Javaでintを文字列に変換するにはどうすればよいですか?
整数から文字列への変換には、以下に示す特定の方法があります。
- の使用 toString() Integerクラスのメソッド
- の使用 値の() Stringクラスのメソッド
- 使用する 整数(int).toString() Integerクラスのメソッド
- 空の文字列との連結を使用します。
IntegerクラスのtoStringメソッドの使用
Integer クラスには静的メソッドがあります toString() 指定された int パラメータを表す String オブジェクトを返します。引数は変換され、文字列インスタンスとして返されます。数値が負の場合、符号は保持されます。
例:
ジャワ
// 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);> >}> }> |
>
>出力
String str1 = 1234 String str2 = -1234>
StringクラスのvalueOf()メソッドを使用する
String クラスには静的メソッド Java があります。
// Java Program to Illustrate 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>;> > >// 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);> >}> }> |
>
>出力
の Java インスタンス
String str3 = 1234>
Integer クラスの toString() メソッドの使用
このメソッドでは、Integer クラスのインスタンスを使用して toString() メソッドを呼び出すため、上で提案したメソッド 1 とは異なります。
以下は上記のメソッドの実装です。
ジャワ
// Java Program to Illustrate> // Integer to String Conversions> // Using toString() Method of> // Integer Class> > // 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 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);> >}> }> |
コンピューターが発明された年
>
>
出力

出力の説明: 変数がプリミティブ型 (int) の場合は、Integer.toString(int) または String.valueOf(int) を使用することをお勧めします。ただし、変数がすでに Integer (プリミティブ型 int のラッパー クラス) のインスタンスである場合は、上記のように toString() メソッドを呼び出すだけの方がよいでしょう。
注記: 変換が実行される前に Integer クラスのインスタンスが作成されるため、このメソッドは効率的ではありません。
空の文字列との連結の使用
アプローチ: ここでは空の文字列を宣言し、「+」演算子を使用して結果を文字列として単純に保存します。これにより、これらの文字列を正常に追加して連結できるようになりました。
上記のメソッドの実装を以下に示します。
ジャワ
// Java Program to Illustrate 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 concatenated strings> >System.out.println(>'String str1 = '> + str1);> >System.out.println(>'String str2 = '> + str2);> >}> }> |
>
>出力
String str1 = 1234 String str2 = -1234>
Java を文字列に変換する高度なメソッド
以下に示す特定の事前メソッドがあります。
- DecimalFormatクラスの使用
- StringBuffer クラスの使用
- StringBuilderクラスを使用する
- 特殊基数とカスタム基数の使用
DecimalFormatクラスの使用
10 進形式 数値を文字列にフォーマットするクラスです。
上記のメソッドの実装を以下に示します。
ジャワ
// Java Program to Illustrate> // Integer to String Conversions> // Using DecimalFormat Class> > // Importing required 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);> >}> }> |
>
>
出力

ヒント: この方法を使用すると、読みやすくするために小数点以下の桁数とカンマ区切り文字を指定できます。
StringBuffer クラスの使用
StringBuffer は、複数の値を String に連結するために使用されるクラスです。
例 1:
ジャワ
npどこ
// Java Program to Illustrate> // 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>
例 2:
ジャワ
// Java Program to Illustrate> // 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 クラスの使用
文字列ビルダー 同様に動作しますが、StringBuffer のようにスレッドセーフではありません。
例 1:
ジャワ
// Java Program to Illustrate> // 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() 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 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>
注記: 上記のすべての例では、基数 (基数) 10 を使用しています。次に、2 進数、8 進数、および 16 進数に変換する便利な方法を示します。任意のカスタム番号システムもサポートされています。
A. 特殊な基数の使用
例: バイナリ
ジャワ
// Java Program to Illustrate 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 の 2 進数表現です。
例: 8進数
ジャワ
// Java Program to Illustrate 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 を 8 進数で表現したものです。
例: 16進数
ジャワ
// Java Program to Illustrate 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);> >}> }> |
>
マップC++で見つける
>出力
ff>
の ff 数値 255 を 16 進数で表現したものです。
B. カスタム基数/基数
アプローチ: Integer クラスの toString() メソッドを使用して文字列に変換し、さらに radix と呼ばれる引数として値を渡します。 int を文字列に変換するときに、他のカスタム基数/基数を使用できます。以下の例では、説明のために 7 進数の数値体系を考慮しています。
例:
ジャワ
// Java Program to Illustrate 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 は、7 進法で書かれた場合の数値 255 の表現です。
まとめ
Java で整数を文字列に変換するプロセスには、 toString() と valueOf() 直接変換用の Integer クラスのメソッド、 文字列.format() カスタマイズ可能な書式設定オプションについては、 StringBuilder または StringBuffer 効率的な文字列統合のために。