Java String format() メソッドと例

Javaでは、 String format() メソッド 指定されたものを使用してフォーマットされた文字列を返します 地元 、 指定された フォーマット文字列 、 そして 引数 。このメソッドを使用して文字列を連結すると同時に、出力される連結された文字列をフォーマットすることができます。

String format() の構文

2種類あります 文字列フォーマット() 以下で説明する方法:

public static String format (Locale loc , String form , Object... args ) public static String format (String form , Object... args ) 

パラメーター

 locale: the locale value to be applied on the format() method format: The format of the output string. args: args   specifying the number of arguments for the format string. It may be zero or more. 

戻り値

  • フォーマットされた文字列。

スローされた例外

  • NullPointerException: 形式が null の場合。
  • 不正なフォーマット例外: 指定された形式が不正であるか、引数が不十分な場合。

Java String format() の例

ジャワ




// Java program to demonstrate> // working of format() method> // Main class> class> GFG {> > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom input string to be formatted> > String str => 'techcodeview.com'> ;> > // Concatenation of two strings> > String s> > = String.format(> 'My Company name is %s'> , str);> > // Output is given upto 8 decimal places> > String str2> > = String.format(> 'My answer is %.8f'> ,> 47.65734> );> > // Here answer is supposed to be %15.8f' and> > // '47.65734000' there are 15 spaces> > String str3 = String.format(> 'My answer is %15.8f'> ,> > 47.65734> );> > // Print and display strings> > System.out.println(s);> > System.out.println(str2);> > System.out.println(str3);> > }> }>

出力

My Company name is techcodeview.com My answer is 47.65734000 My answer is 47.65734000 

Java フォーマット指定子

フォーマット指定子

データ・タイプ 出力または戻り値

%a

浮動小数点 浮動小数点数の 16 進数出力を返します。

%b

いかなるタイプ 正しいか間違っているか

%c

キャラクター Unicode 文字

%d

整数 10 進整数

%そうです

浮動小数点 科学表記法の 10 進数

%f

浮動小数点 10進数

%g

浮動小数点 10 進数 (精度と値に応じて科学表記法で表記される可能性があります)

%h

いかなるタイプ hashCode() メソッドからの値の 16 進文字列

%n

なし プラットフォーム固有の行区切り文字

%O

整数 8 進数

%s

いかなるタイプ 文字列値

%t

日付時刻 %t は日付/時刻変換のプレフィックスです。

%バツ

整数 16 進文字列

Java 文字列形式指定子の例

例1

ジャワ




// Java program to demonstrate Concatenation of Arguments> // to the string using format() method> // Main class> class> GFG {> > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom input string to be formatted> > String str1 => 'GFG'> ;> > String str2 => 'techcodeview.com'> ;> > // %1$ represents first argument> > // %2$ second argument> > String str = String.format(> > 'My Company name'> > +> ' is: %1$s, %1$s and %2$s'> ,> > str1, str2);> > // Print and display the formatted string> > System.out.println(str);> > }> }>

出力

My Company name is: GFG, GFG and techcodeview.com 

例 2

ジャワ




// Java program to Illustrate Left Padding> // using format() method> // Main class> class> GFG {> > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom integer number> > int> num => 7044> ;> > // Output is 3 zero's('000') + '7044',> > // in total 7 digits> > String str = String.format(> '%07d'> , num);> > // Print and display the formatted string> > System.out.println(str);> > }> }>

出力

0007044