Java का String.format()
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
public class Main {
public static void main(String[] args) {
String result = String.format("%,.2f", 1234.5); // "1,234.50"
System.out.println(result);
}
}
Login to try C/C++/Java/PHP code in the editor
Locale-Specific Formatting
एक Locale (जैसे Locale.GERMANY) को बिल्कुल पहले argument के रूप में pass करना String.format() को उस region के conventions उपयोग करने को कहता है -- उदाहरण के लिए, यूरोप में ज़्यादातर उपयोग होने वाला decimal comma और thousands-period swap करना।
उदाहरण: Locale-Specific Formatting
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Padding and Alignment
किसी string को %10s (या left-align करने के लिए %-10s) से एक fixed width में padding वह है जो text के columns को -- एक receipt या एक report जैसे -- visually align कराता है भले ही values अलग lengths की हों।
उदाहरण: Padding and Alignment
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
}
}
Login to try C/C++/Java/PHP code in the editor
Reusing Arguments
%1$s जैसा argument index syntax आपको उसी argument को एक से ज़्यादा बार reference करने देता है, या आपके arguments को आपके pass करने से अलग order में print करने देता है, call में उन्हें duplicate किए बिना।
उदाहरण: Reusing Arguments
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक decimal number के साथ
%dया एक integer के साथ%fउपयोग करना, जोIllegalFormatConversionExceptionthrow करता है। - यह भूल जाना कि
String.formatstring return करता है और इसे print नहीं करता। - Default locale नज़रअंदाज़ करना, इसलिए decimals कुछ systems पर dot के बजाय comma उपयोग कर सकते हैं।
- Java
System.out.println()से output print करती है, औरprintfऔरString.format()control करते हैं कि values कैसे formatted हों। ScannerऔरBufferedReaderkeyboard से 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: