-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMethods.java
More file actions
190 lines (173 loc) · 8.27 KB
/
Copy pathStringMethods.java
File metadata and controls
190 lines (173 loc) · 8.27 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package Phase2_MethodsArraysStrings.Strings;
import java.util.Arrays;
/**
* String Methods - Comprehensive Reference
* ----------------------------------------
* This file is a guided tour through the most-used methods of java.lang.String.
* Each method has a short comment explaining what it does and a runnable
* example with the expected output.
* <p>
*
* Methods Grouped By Purpose
* --------------------------
* <p>
*
* LENGTH / EMPTINESS
* length() - number of UTF-16 code units
* isEmpty() - length == 0
* isBlank() - empty or only whitespace (Java 11+)
* <p>
*
* CHAR ACCESS / ARRAYS
* charAt(i) - char at index i
* toCharArray() - copy to a new char[]
* codePointAt(i) - int code point (supports supplementary chars)
* getBytes() / getBytes(charset) - encode as byte[]
* <p>
*
* SEARCH
* indexOf(ch | str | from) - first occurrence, or -1
* lastIndexOf(ch | str | from)- last occurrence, or -1
* contains(seq) - true if `seq` appears
* startsWith(prefix)
* endsWith(suffix)
* <p>
*
* COMPARISON
* equals(other) - case-sensitive content equality
* equalsIgnoreCase(other)
* compareTo(other) - lexicographic; <0 / 0 / >0
* compareToIgnoreCase(other)
* <p>
*
* CASE / WHITESPACE
* toUpperCase() / toLowerCase()
* trim() - strips ASCII control + space
* strip() / stripLeading() / stripTrailing() (Java 11+, Unicode-aware)
* <p>
*
* TRANSFORM
* replace(old, new) - replaces ALL (chars OR CharSequences)
* replaceFirst(regex, new) - regex first match
* replaceAll(regex, new) - regex all matches
* substring(begin) / substring(begin, end)
* concat(other)
* repeat(count) - Java 11+
* indent(n) - Java 12+
* transform(fn) - Java 12+, apply any Function<String, R>
* <p>
*
* SPLIT / JOIN / FORMAT
* split(regex)
* String.join(delim, parts...) (static)
* String.format(fmt, args...) (static; printf-style)
* formatted(args...) (Java 15+, instance form)
* lines() (Java 11+, Stream of lines)
* <p>
*
* CONVERSION FROM OTHER TYPES
* String.valueOf(int|double|boolean|Object|char[])
* Integer.parseInt(s), Double.parseDouble(s) ...
* <p>
*
* REGEX QUICK PATH
* matches(regex) - matches() must match the WHOLE string
* <p>
*
* Pitfalls Quick Reference
* ------------------------
* - `==` compares references, not content. Use `.equals(...)`.
* - `replace()` takes a literal; `replaceAll()` takes a regex.
* - `split("|")` splits on EVERY char because `|` is a regex meta-character.
* Use `split("\\|")` or `Pattern.quote("|")`.
* - `substring(begin, end)` is HALF-OPEN: includes begin, excludes end.
* - `length()` counts UTF-16 code UNITS, not grapheme clusters. Emojis and
* other supplementary characters take 2 units.
*/
public class StringMethods {
public static void main(String[] args) {
String s = " Hello, Java World! ";
section("LENGTH / EMPTINESS");
System.out.println("length() = " + s.length());
System.out.println("isEmpty() = " + s.isEmpty());
System.out.println("isBlank() = " + " \t\n ".isBlank());
section("CHAR ACCESS");
System.out.println("charAt(2) = " + s.charAt(2));
System.out.println("toCharArray()[0] = " + s.toCharArray()[0]);
System.out.println("codePointAt(2) = " + s.codePointAt(2));
System.out.println("getBytes().length= " + s.getBytes().length);
section("SEARCH");
String t = "Hello, World, Hello";
System.out.println("indexOf('o') = " + t.indexOf('o')); // 4
System.out.println("indexOf('o', 5) = " + t.indexOf('o', 5)); // 8
System.out.println("lastIndexOf('o') = " + t.lastIndexOf('o')); // 16
System.out.println("contains 'World' = " + t.contains("World"));
System.out.println("startsWith Hello = " + t.startsWith("Hello"));
System.out.println("endsWith Hello = " + t.endsWith("Hello"));
section("COMPARISON");
System.out.println("equals('hello',Hello) = " + "hello".equals("Hello"));
System.out.println("equalsIgnoreCase = " + "hello".equalsIgnoreCase("HELLO"));
System.out.println("'a'.compareTo('b') = " + "a".compareTo("b")); // -1
System.out.println("'apple'.compareTo('banana') = " + "apple".compareTo("banana"));
section("CASE / WHITESPACE");
System.out.println("toUpperCase() = " + "Java".toUpperCase());
System.out.println("toLowerCase() = " + "Java".toLowerCase());
System.out.println("trim() = '" + s.trim() + "'");
System.out.println("strip() = '" + " hi ".strip() + "'");
System.out.println("stripLeading() = '" + " hi".stripLeading() + "'");
System.out.println("stripTrailing() = '" + "hi ".stripTrailing() + "'");
section("TRANSFORM");
System.out.println("replace(',','-') = " + t.replace(',', '-'));
System.out.println("replace('Hello','Hi') = " + t.replace("Hello", "Hi"));
System.out.println("replaceAll('o','0') = " + t.replaceAll("o", "0"));
System.out.println("replaceFirst('o','0') = " + t.replaceFirst("o", "0"));
System.out.println("substring(7) = " + t.substring(7));
System.out.println("substring(7, 12) = " + t.substring(7, 12));
System.out.println("repeat(3) = " + "ab".repeat(3));
System.out.println("'X'.repeat(0) = '" + "X".repeat(0) + "'");
System.out.println("indent(4) shown below:");
System.out.print( "line1\nline2".indent(4));
System.out.println("transform(toUpper+!) = " + "java".transform(x -> x.toUpperCase() + "!"));
section("SPLIT / JOIN / FORMAT");
String[] toks = "a,b,c,,d".split(",");
System.out.println("split(\",\") = " + Arrays.toString(toks));
System.out.println("split(\",\",-1)= " + Arrays.toString("a,b,c,,d".split(",", -1))); // keep trailing empty
System.out.println("String.join(\"-\", a, b, c) = " + String.join("-", "a", "b", "c"));
System.out.println("String.format = " + String.format("%-5s %3d", "ID", 42));
System.out.println("\"x=%d\".formatted(7) = " + "x=%d".formatted(7));
System.out.println("lines().count() = " + "one\ntwo\nthree".lines().count());
section("CONVERSION");
System.out.println("String.valueOf(3.14) = " + String.valueOf(3.14));
System.out.println("String.valueOf(true) = " + String.valueOf(true));
System.out.println("Integer.parseInt(42) = " + Integer.parseInt("42"));
System.out.println("Double.parseDouble = " + Double.parseDouble("3.14"));
section("REGEX");
System.out.println("'abc123'.matches(\\\\w+) = " + "abc123".matches("\\w+"));
System.out.println("'abc 123'.matches(\\\\w+) = " + "abc 123".matches("\\w+"));
section("COMMON PITFALL EXAMPLES");
// split("|") - | is a regex OR
String[] bad = "a|b|c".split("|");
String[] good = "a|b|c".split("\\|");
System.out.println("split(\"|\") = " + Arrays.toString(bad));
System.out.println("split(\"\\\\|\") = " + Arrays.toString(good));
// == vs equals
String x = new String("java");
String y = "java";
System.out.println("x == y = " + (x == y));
System.out.println("x.equals(y) = " + x.equals(y));
// OUTPUT
// ====== LENGTH / EMPTINESS ======
// length() = 22
// isEmpty() = false
// isBlank() = true
// ====== CHAR ACCESS ======
// charAt(2) = H
// toCharArray()[0] = (a space)
// codePointAt(2) = 72
// getBytes().length= 22
// (... etc - matches the inline comments above ...)
}
private static void section(String title) {
System.out.println("\n====== " + title + " ======");
}
}