strings

Strings


Facts

public final class String implements ... {
    @Stable     // most hyper mega final
    private final byte[] value;
  • Klasse final
  • Strings sind immutable (unveränderbar)
  • Jede Stringoperation returnt entweder
    • den übergebenen String (keine Änderung)
    • einen new String

Stringpool

String a = "literal";
String b = "literal";
String c = new String("literal");

a == b           -> true

a == c           -> false
a.equals(c)      -> true

equals👍


Concatenation

String alphabet = "";
for (char c = 'a'; c <= 'z'; c++)
    alphabet += c;

Verarbeitung Compilersache

⟹ nicht in Schleifen! ⚠️

StringBuilder sb = new StringBuilder();
for (char c = 'a'; c <= 'z'; c++)
    sb.append(c);
String alphabet = sb.toString();

👍


Methoden


"Seperated by delimiter".split(" ") 
-> ["Seperated", "by", "delimiter"]

String.join(" ", "Seperated", "by", "delimiter") 
-> "Seperated by delimiter"

String.format(Locale.US,"%08.3f", 314.15) -> "0314.150"
"%08.3f".formatted(314.15) -> "0314,150"

"String".charAt(2)  -> 'r'
"String".charAt(20) -> IndexOutOfBoundsException 💥

"String".indexOf('r')   -> 2
"String".indexOf('X')   -> -1

"String".length()       -> 6
"🦥🏴‍☠️".length()        -> 7   ⚠️ Emoji ⚠

"String".substring(2, 5)   -> "rin"
[beginIndex, endIndex[
length = endIndix - beginIndex