Java में String Formatting
In this page:
String.format("format %s %d", value1, value2);
System.out.printf("format %s%n", value);
Basic String.format
String.format() text के लिए %s, integers के लिए %d, और decimals के लिए %f जैसे placeholders उपयोग करके एक formatted string बनाता है, फिर आपको finished String store, log, या pass along करने के लिए वापस देता है — यह खुद कुछ print नहीं करता।
उदाहरण: Basic String.format
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String result = String.format("%s is %d years old", "Alice", 30);
// Print a line to the console
System.out.println(result);
}
}
Login to try C/C++/Java/PHP code in the editor
Printing directly with printf
System.out.printf() String.format() जैसा ही exact format-specifier syntax उपयोग करता है, लेकिन एक String return करने के बजाय result सीधे console में लिखता है, जो जब आपको सिर्फ formatted output तुरंत display करना हो तो एक step बचाता है।
उदाहरण: Printing directly with printf
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print formatted text to the console
System.out.printf("%s is %d years old%n", "Alice", 30);
}
}
Login to try C/C++/Java/PHP code in the editor
Formatting Decimal Places
%f specifier floating-point values format करता है, और आप exactly दो decimal places दिखाने के लिए %.2f जैसे एक modifier से precision control कर सकते हैं — इसके बिना, Java default रूप से छह decimal places देता है, जो currency जैसी चीज़ों के लिए शायद ही कभी आपको चाहिए।
उदाहरण: Formatting Decimal Places
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare price and set it to 9.5
double price = 9.5;
// Print formatted text to the console
System.out.printf("%.2f%n", price);
}
}
Login to try C/C++/Java/PHP code in the editor
Integer Padding and Zero Fill
%5d (minimum width, spaces से right-padded) या %05d (zero-padded) जैसे flags आपको numbers के columns align करने देते हैं ताकि वे neatly print हों, जो एक report या एक scoreboard जैसे tabular console output format करते समय मायने रखता है।
उदाहरण: Integer Padding and Zero Fill
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print formatted text to the console
System.out.printf("%5d%n", 42);
// Print formatted text to the console
System.out.printf("%05d%n", 42);
}
}
Login to try C/C++/Java/PHP code in the editor
Formatting Date & Time Elements
%t specifier family date और time values को format करता है, एक conversion character के साथ combined जैसे चार-digit year के लिए Y या दो-digit month के लिए m — आपको हर component के लिए एक %t group चाहिए जो आप display करना चाहते हों, क्योंकि हर एक सिर्फ date का एक टुकड़ा extract करता है।
उदाहरण: Formatting Date & Time Elements
// Import java.util.Calendar so it can be used by its short name
import java.util.Calendar;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
// Print formatted text to the console
System.out.printf("%tY-%tm%n", cal, cal);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक decimal number के लिए
%dउपयोग करना, जोIllegalFormatConversionExceptionthrow करता है। printfसे कई lines print करते समय%nया\nभूल जाना।- Format specifiers से कम arguments pass करना, जो
MissingFormatArgumentExceptionthrow करता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: