-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModernStringFeatures.java
More file actions
145 lines (126 loc) · 5.85 KB
/
Copy pathModernStringFeatures.java
File metadata and controls
145 lines (126 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package Phase2_MethodsArraysStrings.Strings;
/**
* Modern String Features (Java 11 -> 21)
* --------------------------------------
* Java keeps adding small but useful String methods. This file is a quick
* tour of everything Basics-level that landed in 11, 12, 15, and beyond -
* all available on Java 21.
* <p>
*
* Java 11
* -------
* isBlank() - true if empty OR only whitespace
* lines() - Stream<String> of lines (no separators)
* strip() / stripLeading() / - Unicode-aware trim (smarter than trim())
* stripTrailing()
* repeat(int count) - concat the string with itself N times
* chars() / codePoints() - IntStream over the characters
* <p>
*
* Java 12
* -------
* indent(int n) - add (or remove) leading spaces per line
* transform(Function<String, R>) - apply any function, fluently
* <p>
*
* Java 15
* -------
* Text Blocks """ ... """ - multi-line string literals
* formatted(args) - instance form of String.format
* <p>
*
* Java 21
* -------
* Pattern matching for switch - match on `String` values with `when` guards
* String templates (preview only) - NOT covered here, the feature was
* retracted from later releases.
* <p>
*
* Why These Matter
* ----------------
* - `strip()` handles Unicode whitespace - `trim()` only strips ASCII < 0x20.
* - `repeat()` is much faster than a loop with `+=`.
* - Text blocks make JSON, SQL, HTML in code dramatically easier to read.
* - `transform()` and `lines()` enable a fluent, functional style.
*/
public class ModernStringFeatures {
public static void main(String[] args) {
section("Java 11 - isBlank() vs isEmpty()");
System.out.println("\"\".isEmpty() = " + "".isEmpty()); // true
System.out.println("\"\".isBlank() = " + "".isBlank()); // true
System.out.println("\" \".isEmpty() = " + " ".isEmpty()); // false
System.out.println("\" \".isBlank() = " + " ".isBlank()); // true
System.out.println("\" \\t\\n\".isBlank() = " + " \t\n".isBlank()); // true
section("Java 11 - strip() vs trim()");
// Unicode "FULL WIDTH SPACE" (U+3000) - NOT stripped by trim()
String tricky = " hello ";
System.out.println("trim().length() = " + tricky.trim().length()); // still has
System.out.println("strip().length() = " + tricky.strip().length()); // truly stripped
System.out.println("stripLeading() = '" + " hi".stripLeading() + "'");
System.out.println("stripTrailing() = '" + "hi ".stripTrailing() + "'");
section("Java 11 - repeat()");
System.out.println("\"ab\".repeat(3) = " + "ab".repeat(3)); // ababab
System.out.println("\"-\".repeat(10) = " + "-".repeat(10)); // ----------
System.out.println("\"x\".repeat(0) = '" + "x".repeat(0) + "'"); // empty
// A common use - boxed banners
String title = " SECTION ";
String bar = "=".repeat(title.length());
System.out.println(bar);
System.out.println(title);
System.out.println(bar);
section("Java 11 - lines() : Stream<String>");
String multi = "alpha\nbeta\ngamma";
multi.lines().forEach(System.out::println);
long count = multi.lines().count();
System.out.println("lines count = " + count);
section("Java 11 - chars() / codePoints()");
long vowels = "Hello, World".chars()
.filter(ch -> "aeiouAEIOU".indexOf(ch) >= 0)
.count();
System.out.println("vowel count = " + vowels);
section("Java 12 - indent()");
// indent(n>0) adds n spaces to every line and appends a newline
// indent(n<0) removes up to |n| leading whitespace characters
String code = "if (x) {\n doSomething();\n}";
System.out.print(code.indent(4)); // shifted right by 4 spaces
section("Java 12 - transform()");
String quoted = "java".transform(s -> "<" + s + ">");
System.out.println("transform = " + quoted);
Integer wordCount = "the quick brown fox".transform(s -> s.split(" ").length);
System.out.println("transform to int = " + wordCount);
section("Java 15 - Text Blocks");
String json = """
{
"name": "Deepak",
"skills": ["Java", "Spring"]
}
""";
System.out.println(json);
// Common indentation is stripped automatically - look at the result:
String sql = """
SELECT id, name
FROM users
WHERE active = true
""";
System.out.println(sql);
section("Java 15 - String#formatted (instance form of String.format)");
String msg = "Hello %s, you have %d new messages.".formatted("Deepak", 7);
System.out.println(msg);
section("Java 21 - Pattern matching for switch on String");
for (String input : new String[]{"yes", "no", "maybe", null}) {
String reply = switch (input) {
case null -> "missing";
case "yes", "y" -> "affirmative";
case "no", "n" -> "negative";
case String s when s.startsWith("m") -> "uncertain (" + s + ")";
default -> "unknown";
};
System.out.println("input=" + input + " -> " + reply);
}
// SAMPLE OUTPUT
// (... matches the inline comments above ...)
}
private static void section(String title) {
System.out.println("\n====== " + title + " ======");
}
}