← Back to Core Java Course | Chapter 6: Strings | Lesson 4 of 5

Java में String Formatting

String formatting आपको blanks वाले patterns से tidy text बनाने देता है, एक template भरने जैसा। आप decimals, padding, और dates control करते हैं।
Syntax
java
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

java
// 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);
	}
}

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

java
// 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);
	}
}

Formatting Decimal Places

%f specifier floating-point values format करता है, और आप exactly दो decimal places दिखाने के लिए %.2f जैसे एक modifier से precision control कर सकते हैं — इसके बिना, Java default रूप से छह decimal places देता है, जो currency जैसी चीज़ों के लिए शायद ही कभी आपको चाहिए।

उदाहरण: Formatting Decimal Places

java
// 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);
	}
}

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

java
// 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);
	}
}

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

java
// 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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक decimal number के लिए %d उपयोग करना, जो IllegalFormatConversionException throw करता है।
  2. printf से कई lines print करते समय %n या \n भूल जाना।
  3. Format specifiers से कम arguments pass करना, जो MissingFormatArgumentException throw करता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.