intermediate language-deep-dive

The String Toolkit

Strings have a rich set of built-in methods. This tutorial covers the ones you will use most — not a complete reference, but the practical toolkit for real programs: parsing input, transforming text, and splitting data.

Part of: How Clean works

Inspection and normalization — the first thing to do with any user input:

start:
	string raw = "  Hello, Clean Language!  "
	string clean = raw.trim()

	print("Length before: " + raw.length().toString())
	print("Length after: " + clean.length().toString())
	print("Upper: " + clean.toUpperCase())
	print("Lower: " + clean.toLowerCase())
	print("Has 'Clean': " + clean.contains("Clean").toString())
	print("Starts with 'Hello': " + clean.startsWith("Hello").toString())
	print("Ends with '!': " + clean.endsWith("!").toString())
Length before: 26
Length after: 22
Upper: HELLO, CLEAN LANGUAGE!
Lower: hello, clean language!
Has 'Clean': true
Starts with 'Hello': true
Ends with '!': true

.trim() removes leading and trailing whitespace — always call it on user input before doing anything else. .contains(), .startsWith(), and .endsWith() return boolean and work directly in if conditions.

Transformation and parsing — working with structured text:

start:
	string title = "Hello World From Clean Language"
	string slug = title.toLowerCase().replace(" ", "-")
	print(slug)

	string csv = "alice,bob,charlie,diana"
	list<string> names = csv.split(",")
	iterate name in names
		print("- " + name.trim())

	string code = "PROMO2026"
	string prefix = code.substring(0, 5)
	string suffix = code.substring(5, 9)
	print("Prefix: " + prefix + ", Suffix: " + suffix)
hello-world-from-clean-language
- alice
- bob
- charlie
- diana
Prefix: PROMO, Suffix: 2026

.replace(from, to) swaps every occurrence — method calls chain naturally. .split(delimiter) returns a list<string>, which you can iterate immediately. .substring(start, end) extracts part of the text by character position.

  • .trim() removes whitespace from both ends — always use on user input
  • .contains(), .startsWith(), .endsWith() return boolean for use in if conditions
  • .replace(from, to) replaces all occurrences — chain with other methods
  • .split(delimiter) returns list<string> — iterate over the result immediately
📚 → Book 1, Chapter 4 : Designing Classes →
Copied!