0Pricing
Scala for Backend Engineering & Functional Programming · Lektion

Häufige String-Methoden

Teilen, trimmen und transformieren Sie Text.

Häufige String-Methoden ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Scala for Backend Engineering & Functional Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Useful String Methods

Scala strings come with many built-in methods for searching, changing case, and trimming.

Remember that strings are immutable, so each method returns a new string rather than changing the original.

val s = "Scala"
println(s.length)
println(s.toUpperCase)

Changing Case

Use toUpperCase and toLowerCase to convert the case of every letter.

These are handy for case-insensitive comparisons or normalizing input.

val name = "Backend"
println(name.toUpperCase)
println(name.toLowerCase)

Trimming Whitespace

trim removes spaces and other whitespace from the start and end of a string.

It is commonly used to clean up user input.

val raw = "   hello   "
val clean = raw.trim
println("[" + clean + "]")
println(clean.length)

Checking Contents

contains tells you whether one string appears inside another. It returns a Boolean.

The check is case sensitive.

val s = "functional"
println(s.contains("fun"))
println(s.contains("FUN"))

startsWith and endsWith

startsWith and endsWith test whether a string begins or finishes with a given piece.

They are useful for file extensions, prefixes, and URLs.

val file = "report.csv"
println(file.startsWith("report"))
println(file.endsWith(".csv"))

Finding a Position

indexOf returns the index where a character or substring first appears, or -1 if it is not found.

Indexing starts at 0.

val s = "scala"
println(s.indexOf("a"))
println(s.indexOf("z"))

Taking a Substring

substring(start) returns the text from an index to the end. substring(start, end) returns the text between two indexes, not including end.

This lets you slice out part of a string.

val s = "backend"
println(s.substring(0, 4))
println(s.substring(4))

Replacing Text

replace swaps every occurrence of one piece of text with another and returns a new string.

The original string stays unchanged.

val s = "a-b-c"
val dashed = s.replace("-", "_")
println(dashed)
println(s)

Splitting a String

split breaks a string into an array of pieces using a separator.

This is common when parsing CSV lines or splitting words.

val csv = "red,green,blue"
val parts = csv.split(",")
println(parts.length)
println(parts(1))

Chaining Methods

Because each method returns a string, you can chain several calls together.

Read the chain left to right: each result feeds into the next method.

object Main extends App {
  val input = "  Hello World  "
  val result = input.trim.toLowerCase.replace(" ", "-")
  println(result)
}

Converting to a Number

Use toInt or toDouble to parse a numeric string into a number.

This only works if the string actually holds a valid number, otherwise it throws an error.

val s = "123"
val n = s.toInt
println(n + 1)
println("3.5".toDouble)

Quick Check

What does indexOf return when the value is missing?

Recap

You explored common string methods: toUpperCase, toLowerCase, trim, contains, startsWith, and endsWith.

You also saw indexOf, substring, replace, split, and number parsing with toInt. Methods return new strings, so they chain nicely.

Häufig gestellte Fragen

Ist die Lektion „Häufige String-Methoden“ kostenlos?

Ja — der vollständige Text von „Häufige String-Methoden“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Scala for Backend Engineering & Functional Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Häufige String-Methoden“?

Teilen, trimmen und transformieren Sie Text. Du übst Scala for Backend Engineering & Functional Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Scala for Backend Engineering & Functional Programming zu starten?

Keine Vorkenntnisse erforderlich. Scala for Backend Engineering & Functional Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Häufige String-Methoden“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Scala for Backend Engineering & Functional Programming-Lektion Code schreiben und ausführen?

Ja. Jede Scala for Backend Engineering & Functional Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Grundlagen von Strings
  2. s-, f- und Raw-Interpolatoren
  3. Häufige String-Methoden
  4. Mehrzeilige Strings
← Zurück zu Scala for Backend Engineering & Functional Programming