使用 % 在 Java 中進行字串格式化
以下程式碼使用 % 在 Java 中實現了字串格式化 −
示例
public class Demo {
public static void main(String args[]){
String my_str = " sample.";
String concat_Str = String.format("This is a" + "%s", my_str);
String format_str_1 = String.format("The value is %.4f", 78.92367);
System.out.println(concat_Str);
System.out.println(format_str_1);
}
}輸出
This is a sample. The value is 78.9237
名為 Demo 的類包含主函式。在此處定義了一個字串值,用於透過將其連線到另一個變數來格式化該字串。同樣,還使用“%”運算子格式化了浮點數。這兩個值都列印在控制檯上。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP