Skip to content

Commit 33ce708

Browse files
committed
Add English versions of the String examples
The English article needs listings with English text. The classes in basics.en are copies of basics with the German prose strings translated; examples whose point is German text (ß, Grüße) or an explicit Locale.GERMANY stay as they are. The thousands example in the formatting class uses Locale.US.
1 parent cf3a418 commit 33ce708

13 files changed

Lines changed: 364 additions & 1 deletion

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The code uses Java 25 language features and needs Java 25 or newer. Build it wit
88

99
## Java String – `eu.happycoders.string`
1010

11-
The examples of the article are in `eu.happycoders.string.basics`, one class per section with a `main()` method that prints the outputs shown in the article (`Intro`, then `Ch1Creating` to `Ch11Internals`). The `equalsFoldCase()` example is not among them: the method exists since Java 26, and the repository stays on Java 25.
11+
The examples of the article are in `eu.happycoders.string.basics`, one class per section with a `main()` method that prints the outputs shown in the article (`Intro`, then `Ch1Creating` to `Ch11Internals`). These classes belong to the German article. The English article uses the same classes in `eu.happycoders.string.basics.en`, with English text wherever the German text is not the point of the example. The `equalsFoldCase()` example is not among them: the method exists since Java 26, and the repository stays on Java 25.
1212

1313
Memory measurements in `eu.happycoders.string.benchmark` (not JMH; class histograms after a full garbage collection):
1414

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package eu.happycoders.string.basics.en;
2+
3+
import java.math.BigDecimal;
4+
import java.nio.charset.StandardCharsets;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
public class Ch10Converting {
9+
10+
static void main() {
11+
int number = Integer.parseInt("42");
12+
String text = String.valueOf(number);
13+
System.out.println(number + 1);
14+
System.out.println(text + 1);
15+
16+
double d = Double.parseDouble("3.14");
17+
System.out.println(d * 2);
18+
System.out.println(String.valueOf(true) + " " + String.valueOf('x'));
19+
20+
System.out.println("== char[]");
21+
String java = "Java";
22+
char[] chars = java.toCharArray();
23+
chars[0] = 'L';
24+
System.out.println(java);
25+
26+
String lava = new String(chars);
27+
chars[0] = 'J';
28+
System.out.println(lava);
29+
30+
System.out.println("== byte[]");
31+
byte[] utf8 = "Grüße".getBytes(StandardCharsets.UTF_8);
32+
System.out.println(utf8.length);
33+
System.out.println(new String(utf8, StandardCharsets.UTF_8));
34+
35+
System.out.println("== Array and list");
36+
String[] parts = "Ada,Grace,Alan".split(",");
37+
List<String> list = Arrays.asList(parts);
38+
System.out.println(list);
39+
System.out.println(String.join(";", list));
40+
41+
System.out.println("== transform()");
42+
BigDecimal price = " 19.99 ".strip().transform(BigDecimal::new);
43+
System.out.println(price.multiply(BigDecimal.TWO));
44+
}
45+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package eu.happycoders.string.basics.en;
2+
3+
public class Ch11Internals {
4+
5+
static void main() {
6+
String a = "Java";
7+
String b = "Java";
8+
String c = new String("Java");
9+
String d = "Ja" + "va";
10+
String prefix = "Ja";
11+
String e = prefix + "va";
12+
13+
System.out.println(a == b);
14+
System.out.println(a == c);
15+
System.out.println(a == d);
16+
System.out.println(a == e);
17+
18+
final String constantPrefix = "Ja";
19+
System.out.println(a == constantPrefix + "va");
20+
21+
System.out.println("== intern()");
22+
System.out.println(a == c.intern());
23+
System.out.println(a == e.intern());
24+
25+
System.out.println("== hashCode()");
26+
System.out.println("Java".hashCode());
27+
System.out.println("Aa".hashCode() + " " + "BB".hashCode());
28+
System.out.println("".hashCode());
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package eu.happycoders.string.basics.en;
2+
3+
import java.nio.charset.StandardCharsets;
4+
5+
public class Ch1Creating {
6+
7+
static void main() {
8+
String literal = "Java";
9+
10+
char[] chars = {'J', 'a', 'v', 'a'};
11+
String fromChars = new String(chars);
12+
13+
byte[] bytes = {74, 97, 118, 97};
14+
String fromBytes = new String(bytes, StandardCharsets.UTF_8);
15+
16+
System.out.println(literal + " " + fromChars + " " + fromBytes);
17+
18+
System.out.println("== Escape sequences");
19+
System.out.println("She said: \"Hello\"");
20+
System.out.println("C:\\Users\\ada");
21+
System.out.println("Name:\tAda\nName:\tGrace");
22+
System.out.println("Coffee: \u2615");
23+
24+
System.out.println("== Text block");
25+
String json =
26+
"""
27+
{
28+
"name": "Ada"
29+
}
30+
""";
31+
System.out.print(json);
32+
}
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package eu.happycoders.string.basics.en;
2+
3+
public class Ch2Immutable {
4+
5+
static void main() {
6+
String name = "ada";
7+
name.toUpperCase();
8+
System.out.println(name);
9+
10+
name = name.toUpperCase();
11+
System.out.println(name);
12+
}
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package eu.happycoders.string.basics.en;
2+
3+
public class Ch3Length {
4+
5+
static void main() {
6+
String word = "Java";
7+
System.out.println(word.length());
8+
System.out.println(word.charAt(0) + " " + word.charAt(3));
9+
10+
System.out.println("== isEmpty() and isBlank()");
11+
System.out.println("".isEmpty() + " " + "".isBlank());
12+
System.out.println(" ".isEmpty() + " " + " ".isBlank());
13+
14+
System.out.println("== Iterating");
15+
for (int i = 0; i < word.length(); i++) {
16+
System.out.print("[" + word.charAt(i) + "]");
17+
}
18+
System.out.println();
19+
20+
for (char c : word.toCharArray()) {
21+
System.out.print("[" + c + "]");
22+
}
23+
System.out.println();
24+
25+
long count = word.chars().filter(c -> c == 'a').count();
26+
System.out.println(count);
27+
28+
System.out.println("== Emoji");
29+
String text = "Hi 👋";
30+
System.out.println(text.length());
31+
System.out.println(text.codePointCount(0, text.length()));
32+
System.out.println(text.codePoints().count());
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package eu.happycoders.string.basics.en;
2+
3+
import java.util.Locale;
4+
5+
public class Ch4Searching {
6+
7+
static void main() {
8+
String sentence = "A String is a sequence of characters";
9+
10+
System.out.println(sentence.contains("sequence"));
11+
System.out.println(sentence.indexOf("a"));
12+
System.out.println(sentence.indexOf("a", 13));
13+
System.out.println(sentence.lastIndexOf("e"));
14+
System.out.println(sentence.indexOf("Java"));
15+
System.out.println(sentence.startsWith("A") + " " + sentence.endsWith("characters"));
16+
17+
System.out.println("== Ignoring case");
18+
System.out.println(sentence.contains("string"));
19+
System.out.println(sentence.toLowerCase(Locale.ROOT).contains("string"));
20+
21+
System.out.println("== matches()");
22+
System.out.println("12345".matches("\\d+"));
23+
System.out.println("123a5".matches("\\d+"));
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package eu.happycoders.string.basics.en;
2+
3+
import java.util.Arrays;
4+
5+
public class Ch5Splitting {
6+
7+
static void main() {
8+
String name = "Ada Lovelace";
9+
System.out.println(name.substring(4));
10+
System.out.println(name.substring(0, 3));
11+
12+
System.out.println("== trim() and strip()");
13+
String padded = "\u2003Ada\u2003";
14+
System.out.println(padded.length());
15+
System.out.println(padded.trim().length());
16+
System.out.println(padded.strip().length());
17+
18+
System.out.println("== split()");
19+
String csv = "Ada,Grace,,Alan,,";
20+
System.out.println(Arrays.toString(csv.split(",")));
21+
System.out.println(Arrays.toString(csv.split(",", -1)));
22+
System.out.println(Arrays.toString(csv.split(",", 2)));
23+
24+
System.out.println(Arrays.toString("1.2.3".split(".")));
25+
System.out.println(Arrays.toString("1.2.3".split("\\.")));
26+
System.out.println(Arrays.toString("a|b".split("|")));
27+
28+
System.out.println(Arrays.toString("1+2-3".splitWithDelimiters("[+-]", 0)));
29+
30+
System.out.println("== lines()");
31+
System.out.println("one\ntwo\r\nthree".lines().toList());
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package eu.happycoders.string.basics.en;
2+
3+
import java.util.Locale;
4+
5+
public class Ch6Changing {
6+
7+
static void main() {
8+
System.out.println("1.2.3".replace('.', '_'));
9+
System.out.println("1.2.3".replace(".", ""));
10+
System.out.println("1.2.3".replaceAll(".", ""));
11+
System.out.println("1.2.3".replaceAll("\\.", ""));
12+
13+
System.out.println("a1b22c333".replaceAll("\\d+", "#"));
14+
System.out.println("a1b22c333".replaceFirst("\\d+", "#"));
15+
16+
System.out.println("Hello".replace("l", ""));
17+
18+
System.out.println("== Upper and lower case");
19+
System.out.println("Straße".toUpperCase(Locale.ROOT));
20+
Locale turkish = Locale.forLanguageTag("tr");
21+
System.out.println("title".toUpperCase(turkish));
22+
System.out.println("TITLE".toLowerCase(turkish));
23+
System.out.println("title".toUpperCase(Locale.ROOT));
24+
25+
System.out.println("== repeat() and reverse");
26+
System.out.println("-".repeat(10));
27+
System.out.println(new StringBuilder("Hello").reverse().toString());
28+
29+
System.out.println("== stripIndent() and indent()");
30+
String sql = " SELECT name\n FROM users";
31+
System.out.println(sql);
32+
System.out.println(sql.stripIndent());
33+
System.out.print(sql.stripIndent().indent(2));
34+
35+
System.out.println("== translateEscapes()");
36+
String raw = "Name:\\tAda\\nName:\\tGrace";
37+
System.out.println(raw);
38+
System.out.println(raw.translateEscapes());
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package eu.happycoders.string.basics.en;
2+
3+
import java.util.Objects;
4+
5+
public class Ch7Comparing {
6+
7+
static void main() {
8+
String language = "Java";
9+
String prefix = "Ja";
10+
String built = prefix + "va";
11+
12+
System.out.println(language == built);
13+
System.out.println(language.equals(built));
14+
System.out.println(language.equalsIgnoreCase("JAVA"));
15+
16+
System.out.println("== compareTo()");
17+
System.out.println("Ada".compareTo("Alan"));
18+
System.out.println("ada".compareTo("Alan"));
19+
System.out.println("Ada".compareTo("Ada"));
20+
21+
System.out.println("== null");
22+
String input = null;
23+
System.out.println("Java".equals(input));
24+
System.out.println(Objects.equals(input, "Java"));
25+
try {
26+
System.out.println(input.equals("Java"));
27+
} catch (NullPointerException e) {
28+
System.out.println(e.getClass().getName());
29+
}
30+
31+
System.out.println("== switch");
32+
String command = "stop";
33+
switch (command) {
34+
case "start" -> System.out.println("Starting ...");
35+
case "stop" -> System.out.println("Stopping ...");
36+
default -> System.out.println("Unknown: " + command);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)