0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Common String Methods

Split, trim, and transform text.

Common String Methods is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Common String Methods” lesson free?

Yes — the full text of “Common String Methods” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Common String Methods”?

Split, trim, and transform text. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Common String Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. String Basics
  2. s, f and raw Interpolators
  3. Common String Methods
  4. Multiline Strings
← Back to Scala for Backend Engineering & Functional Programming