Followimg เป็นโค้ดสำหรับใช้การจัดรูปแบบสตริงใน 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 มีฟังก์ชันหลัก ค่าสตริงถูกกำหนด ซึ่งใช้ในการจัดรูปแบบสตริง โดยการต่อกับตัวแปรอื่น ในทำนองเดียวกัน หมายเลขทศนิยมยังถูกจัดรูปแบบโดยใช้ตัวดำเนินการ '%' ค่าทั้งสองนี้จะพิมพ์อยู่บนคอนโซล