Die wichtigsten String-Operationen
Länge
"Hallo".length() // 5
len("Hallo") # 5
Substring / Slicing
Bekommst einen Teil-String. Achtung — end ist EXKLUSIV (geht bis aber nicht inklusive).
String s = "Hallo Welt";
s.substring(0, 5); // "Hallo" (Index 0,1,2,3,4 — NICHT 5)
s.substring(6, 10); // "Welt"
s.substring(6); // "Welt" (ohne end → bis zum Ende)
s = "Hallo Welt"
s[0:5] # "Hallo"
s[6:10] # "Welt"
s[6:] # "Welt" (ohne end → bis Ende)
s[:5] # "Hallo" (ohne start → ab Anfang)
Python-Slicing ist mächtiger: s[::2] jedes zweite, s[::-1] reversed.
Konkatenation: +
"Hallo" + " " + "Welt" // "Hallo Welt"
"Hallo" + " " + "Welt" # "Hallo Welt"
Suchen: indexOf / find
"Hallo Welt".indexOf("Welt") // 6
"Hallo Welt".indexOf("xyz") // -1 (nicht gefunden)
"Hallo Welt".find("Welt") # 6
"Hallo Welt".find("xyz") # -1
"Welt" in "Hallo Welt" # True (einfacher!)
Ersetzen: replace
"Hallo Welt".replace("Welt", "Du") // "Hallo Du"
"Hallo Welt".replace("Welt", "Du") # "Hallo Du"
Groß/Klein
"Hallo".toUpperCase() // "HALLO"
"Hallo".toLowerCase() // "hallo"
"Hallo".upper() # "HALLO"
"Hallo".lower() # "hallo"
Splitten + Joinen
String[] parts = "a,b,c".split(","); // ["a", "b", "c"]
String joined = String.join("-", parts); // "a-b-c"
parts = "a,b,c".split(",") # ["a", "b", "c"]
joined = "-".join(parts) # "a-b-c"