How do I replace or transform text in a string in EK9?
← Common String Operations · Ref: Q175
EK9 strings do not have a .replace() method. Instead, use split + stream rejoin for pattern-based replacement, or stream characters with map/filter for character-level transformations.
SPLIT AND REJOIN
Break at a pattern and collect back together:
parts <- sentence.split(/,/)
Split returns a List of String. Rejoin the parts using stream collect:
rejoined <- cat parts | collect as String
CHARACTER STREAMING
Strings are streamable as character sequences. Transform each character:
cat myString | map with toUpper | collect as String
Filter out specific characters:
cat myString | filter by isNotSpace | collect as String
WHY NO REPLACE
EK9's philosophy is that strings are streamable sequences. Rather than adding dozens of specialized methods, EK9 leverages the universal stream pipeline for transformations.
See Q37 for string streaming. See Q33 for regular expressions. See Q89 for stream pipelines.
Example
defines module qa.stringops.transform defines function isNotComma() as pure -> ch as Character <- rtn as Boolean: ch <> ',' charToUpper() as pure -> ch as Character <- rtn as String: $ch.upperCase() isLetter() as pure -> ch as Character <- rtn as Boolean: $ch matches /[A-Za-z]/ defines program StringTransformDemo() stdout <- Stdout() // === SPLIT AND REJOIN === csv <- "Alice,Bob,Charlie" parts <- csv.split(/,/) stdout.println(`Split: ${parts}`) // Rejoin without commas rejoined <- cat parts | collect as String stdout.println(`Rejoined: ${rejoined}`) // === CHARACTER STREAMING — FILTER === // Remove commas by filtering characters noCommas <- cat csv | filter by isNotComma | collect as String stdout.println(`No commas: ${noCommas}`) // === CHARACTER STREAMING — MAP === // Transform to uppercase via character stream lower <- "hello world" upper <- cat lower | map with charToUpper | collect as String stdout.println(`Uppercased via stream: ${upper}`) // === EXTRACT ONLY LETTERS === messy <- "h3ll0 w0rld!" lettersOnly <- cat messy | filter by isLetter | collect as String stdout.println(`Letters only: ${lettersOnly}`) // === SPLIT WITH REGEX === sentence <- "one two three" words <- sentence.split(/ +/) stdout.println(`Words: ${words}`)
Common mistakes
E50060 — String has no replace() method. Use split + stream collect, or stream characters with filter/map for transformations. See ek9 -h E50060 for details.
Incorrect:
parts <- csv.replace(",", " ")
Correct:
parts <- csv.split(/,/)
Other ways to ask this
- Does EK9 have a string replace method?
- How do I substitute text in a string in EK9?
- How do I transform characters in a string in EK9?
Coming from another language?
Java: str.replace(), str.replaceAll(regex). Python: str.replace(), re.sub(). Rust: str.replace(), str.replacen(). Go: strings.Replace(), strings.ReplaceAll(). JavaScript: str.replace(), str.replaceAll(). Kotlin: str.replace(), str.replace(regex). EK9: no .replace(), use split(regex) + collect, or stream characters with map/filter.
Keywords: characters, substitute, replace, split, string, map, text, transform, regex, filter, stream