String Formatting
CoddyのJavaジャーニー「基礎」セクションの一部 — レッスン 69/73。
Java では、String.format() は、フォーマットされた文字列を作成するために使用される強力なメソッドです。これにより、テキストと変数を読みやすくカスタマイズ可能な方法で結合できます。このメソッドは、変数が文字列に挿入される方法を定義するためのフォーマット指定子を使用します。
String.format() の基本構文は:
String formattedString = String.format("format_string", arg1, arg2, ...);format_string: テキストとフォーマット指定子を含む文字列。arg1, arg2, ...:format_stringに挿入される変数。
以下は一般的なフォーマット指定子です:
%s:文字列を挿入します。%d:10進数(整数)を挿入します。%f:浮動小数点数を挿入します。
%b: ブール値を挿入します。%c: 文字を挿入します。%n: 改行文字を挿入します。
さらに書式設定を詳細に制御することもできます:
%.2f: 浮動小数点数を小数点以下2桁で書式設定します。%10s: 文字列を10文字のフィールド内に右揃えで挿入します。
%-10s: 文字列を挿入し、10文字のフィールド内で左揃えします。%03d: 整数を挿入し、3桁の幅になるよう先頭にゼロを埋めます。
こちらは例です:
String name = "Alice";
int age = 30;
double price = 19.99;
String formatted = String.format("Name: %s, Age: %d, Price: %.2f", name, age, price);
System.out.println(formatted);
// Output: Name: Alice, Age: 30, Price: 19.99この例では、%s が name に、%d が age に、%.2f が price に置き換えられ、後者は2桁の小数点以下でフォーマットされます。
チャレンジ
簡単createFormattedString という名前のメソッドを作成し、次の引数を受け取るようにしてください:
- 文字列
productName。 - 整数
quantity。 - double 型の
unitPrice。
このメソッドは、これらの値を次の形式で結合した書式付きの文字列を返すべきです:
Product: [productName], Quantity: [quantity], Unit Price: [unitPrice]unitPrice を小数点以下5桁に、quantity を小数点以下1桁で書式化してください(double に変換)。
例えば、productName が "laptop"、quantity が 3、unitPrice が 1299.9999 の場合、メソッドは次のように返すべきです:
Product: laptop, Quantity: 3.0, Unit Price: 1299.99990String 型のまま最初の文字を抽出するには、char 型ではなく
str.substring(0,1)を使用してください:
チートシート
String.format() メソッドは、フォーマット指定子を使用してテキストと変数を結合して書式付きの文字列を作成します。
基本構文:
String formattedString = String.format("format_string", arg1, arg2, ...);一般的なフォーマット指定子:
%s: 文字列%d: 整数%f: 浮動小数点数%b: ブール値%c: 文字%n: 改行
書式設定オプション:
%.2f: 小数点以下2桁の浮動小数点数%10s: 10文字フィールド内の右揃え文字列%-10s: 10文字フィールド内の左揃え文字列%03d: 先頭に0を詰めて3桁にした整数
例:
String name = "Alice";
int age = 30;
double price = 19.99;
String formatted = String.format("Name: %s, Age: %d, Price: %.2f", name, age, price);
System.out.println(formatted);
// Output: Name: Alice, Age: 30, Price: 19.99最初の文字を文字列として抽出するには:str.substring(0,1)
自分で試してみよう
import java.util.Scanner;
public class Main {
public static String createFormattedString(String productName, int quantity, double unitPrice) {
// ここにコードを書いてください
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String product = scanner.nextLine();
int qty = scanner.nextInt();
double price = scanner.nextDouble();
String formattedString = createFormattedString(product, qty, price);
System.out.println(formattedString);
}
}このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
基礎のすべてのレッスン
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else