← Back to Core Java Course | Chapter 2: Input & Output | Lesson 6 of 6

Java का String.format()

String.format blanks वाले एक pattern से एक nicely formatted text का टुकड़ा बनाता है, एक fill-in-the-blank sentence जैसा। यह आपको text तुरंत print करने के बजाय वापस देता है।
Syntax
java
String result = String.format("format %s %d", stringValue, intValue);

Basic String Formatting

String.format() printf() जैसा ही %s/%d/%f placeholder syntax उपयोग करके एक formatted String value बनाता और return करता है, लेकिन console पर लिखने के बजाय यह result आपको store, log, या pass along करने के लिए देता है।

उदाहरण: Basic String Formatting

java
public class Main {
	public static void main(String[] args) {
		String result = String.format("%s is %d", "Alice", 30); // returned, not printed directly
		System.out.println(result);
	}
}

Numeric Formatting

%,d जैसे flags thousands separators insert करते हैं (1,000,000) और %.2f decimal precision control करता है, आपको 1234.5 जैसे raw double को display के लिए properly formatted "1,234.50" में बदलने देते हुए।

उदाहरण: Numeric Formatting

java
public class Main {
	public static void main(String[] args) {
		String result = String.format("%,.2f", 1234.5); // "1,234.50"
		System.out.println(result);
	}
}

Locale-Specific Formatting

एक Locale (जैसे Locale.GERMANY) को बिल्कुल पहले argument के रूप में pass करना String.format() को उस region के conventions उपयोग करने को कहता है -- उदाहरण के लिए, यूरोप में ज़्यादातर उपयोग होने वाला decimal comma और thousands-period swap करना।

उदाहरण: Locale-Specific Formatting

java
import java.util.Locale;
public class Main {
	public static void main(String[] args) {
		String result = String.format(Locale.GERMANY, "%,.2f", 1234.5); // "1.234,50"
		System.out.println(result);
	}
}

Padding and Alignment

किसी string को %10s (या left-align करने के लिए %-10s) से एक fixed width में padding वह है जो text के columns को -- एक receipt या एक report जैसे -- visually align कराता है भले ही values अलग lengths की हों।

उदाहरण: Padding and Alignment

java
public class Main {
	public static void main(String[] args) {
		System.out.println(String.format("[%10s]", "hi"));  // right-aligned, width 10
		System.out.println(String.format("[%-10s]", "hi")); // left-aligned
	}
}

Reusing Arguments

%1$s जैसा argument index syntax आपको उसी argument को एक से ज़्यादा बार reference करने देता है, या आपके arguments को आपके pass करने से अलग order में print करने देता है, call में उन्हें duplicate किए बिना।

उदाहरण: Reusing Arguments

java
public class Main {
	public static void main(String[] args) {
		String result = String.format("%1$s is %1$s again", "Java"); // reuses argument 1 twice
		System.out.println(result);
	}
}
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 या एक integer के साथ %f उपयोग करना, जो IllegalFormatConversionException throw करता है।
  2. यह भूल जाना कि String.format string return करता है और इसे print नहीं करता।
  3. Default locale नज़रअंदाज़ करना, इसलिए decimals कुछ systems पर dot के बजाय comma उपयोग कर सकते हैं।
चैप्टर सारांश
  • Java System.out.println() से output print करती है, और printf और String.format() control करते हैं कि values कैसे formatted हों।
  • Scanner और BufferedReader keyboard से user input पढ़ते हैं।
  • सही input और output tools चुनना programs को interactive और उनकी output readable बनाता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.