Java के Text Blocks
In this page:
String text = """
first line
second line
""";
Introduction to Text Blocks
Text blocks आपको backslash line-continuation characters या अंतहीन string concatenation की ज़रूरत के बिना multi-line string literals लिखने देते हैं। एक text block तीन double quotes (""") से शुरू और खत्म होता है, और उन delimiters के बीच सब कुछ जैसे लिखा गया है वैसे ही बना रहता है, newlines सहित।
उदाहरण: Introduction to Text Blocks
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String text = """
Line one
Line two""";
// Print a line to the console
System.out.println(text);
}
}
Login to try C/C++/Java/PHP code in the editor
Indentation Handling
Java किसी text block के अंदर indentation को अपने आप handle करता है: compile time पर, यह हर line द्वारा share किए गए common leading whitespace को तय करता है और हर एक से बिल्कुल उतना ही हटा देता है। यह आपको text block को अपने आस-पास के code से मेल खाने के लिए indent करने देता है बिना उस indentation के असली string value में लीक हुए।
उदाहरण: Indentation Handling
public class Main {
public static void main(String[] args) {
String text = """
Hello
World""";
System.out.println(text); // common leading whitespace is stripped
}
}
Login to try C/C++/Java/PHP code in the editor
Escape Sequences
आप किसी text block के अंदर साधारण double quotes को बिना escape किए उपयोग कर सकते हैं, क्योंकि triple-quote delimiters वह अस्पष्टता हटा देते हैं। अगर आप output में एक असली line break दिखाए बिना text की एक लंबी logical line लिखना चाहते हैं, तो आप newline को suppress करने के लिए उस line को backslash (\\) से खत्म कर सकते हैं।
उदाहरण: Escape Sequences
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String text = """
She said "hello" without escaping. \
This line joins the one above.""";
// Print a line to the console
System.out.println(text);
}
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Formatting
आप block को एक template की तरह उपयोग करते हुए String.format() से, या साफ़ syntax के साथ बराबर behavior के लिए सीधे text block पर नए formatted() instance method को call करके runtime पर text block में values को dynamically format कर सकते हैं।
उदाहरण: Dynamic Formatting
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String template = """
Name: %s
Age: %d""";
// Print a line to the console
System.out.println(template.formatted("Zoya", 28));
}
}
Login to try C/C++/Java/PHP code in the editor
Replacing HTML Templates
Text blocks Java source code के अंदर सीधे embedded HTML, SQL, या JSON templates लिखना और बनाए रखना कहीं आसान बना देते हैं, क्योंकि उन formats की multi-line structure अब visually संरक्षित की जा सकती है बजाय escaped, पढ़ने में मुश्किल single-line strings में flatten होने के।
उदाहरण: Replacing HTML Templates
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String html = """
<html>
<body>Hello</body>
</html>""";
// Print a line to the console
System.out.println(html);
}
}
Login to try C/C++/Java/PHP code in the editor
- opening
"""की उसी line पर text रखना, जैसे"""Hello, जो एक compile error है क्योंकि content को अगली line से शुरू होना चाहिए। - यह न समझना कि closing
"""की position मायने रखती है: अगर यह अपनी ही line पर हो तो string एक newline के साथ खत्म होती है और यह यह भी तय करता है कि कितनी indentation हटाई जाएगी। - block के अंदर बिना escape किए तीन double quotes लिखना, जैसे
\""", क्योंकि लगातार तीन text block को खत्म कर देंगे।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: