How do I check if a string starts or ends with a value in EK9?

← Common String Operations · Ref: Q177

EK9 does not have Swift/Go-style .hasPrefix() or .hasSuffix() methods. Use the matches operator with regex anchors for prefix and suffix checks.

STARTS WITH (PREFIX)

Use regex with ^ anchor:

  if myString matches /^Hello.*/
    stdout.println("Starts with Hello")

ENDS WITH (SUFFIX)

Use regex with $ anchor:

  if myString matches /.*World$/
    stdout.println("Ends with World")

FIRST AND LAST CHARACTER

For single character checks, use the first() and last() methods:

  firstChar <- myString.first()
  lastChar <- myString.last()

WHY NO HASPREFIX/HASSUFFIX

EK9 keeps the String API focused. The matches operator with regex provides the same functionality with more flexibility: you can match complex patterns, not just fixed prefixes.

See Q37 for string methods. See Q33 for regular expressions. See Q178 for first/last character access.

See Q37 for strings. See Q171 for string contains.

Example

defines module qa.stringops.startsends

  defines program
    StringStartsEndsDemo()
      stdout <- Stdout()

      greeting <- "Hello World"

      // === STARTS WITH (PREFIX) ===

      if greeting matches /^Hello.*/
        stdout.println("Starts with Hello")

      if greeting matches /^Goodbye.*/
        stdout.println("Should not print")

      // === ENDS WITH (SUFFIX) ===

      if greeting matches /.*World$/
        stdout.println("Ends with World")

      if greeting matches /.*Earth$/
        stdout.println("Should not print")

      // === FIRST AND LAST CHARACTER ===

      firstCh <- greeting.first()
      lastCh <- greeting.last()
      stdout.println(`First character: ${firstCh}`)
      stdout.println(`Last character: ${lastCh}`)

      // === PRACTICAL: FILE EXTENSION CHECK ===

      filename <- "report.csv"
      if filename matches /.*\.csv$/
        stdout.println("CSV file detected")

      // === PRACTICAL: PROTOCOL CHECK ===

      url <- "https://example.com"
      if url matches /^https.*/
        stdout.println("Secure URL")

Common mistakes

E50060 — String has no hasPrefix() or hasSuffix() methods (common in Swift and Go). Use the matches operator with regex anchors (^, $) for prefix and suffix checks. See ek9 -h E50060 for details.

Incorrect:

if greeting.hasPrefix("Hello")

Correct:

if greeting matches /^Hello.*/
Other ways to ask this
  • Does EK9 have startsWith or endsWith?
  • How do I check for a string prefix in EK9?
  • How do I check for a string suffix in EK9?

Coming from another language?

Java: str.startsWith(prefix), str.endsWith(suffix). Python: str.startswith(prefix), str.endswith(suffix). Rust: str.starts_with(prefix), str.ends_with(suffix). Go: strings.HasPrefix(), strings.HasSuffix(). JavaScript: str.startsWith(), str.endsWith(). Kotlin: str.startsWith(), str.endsWith(). EK9: str matches /^prefix.*/, str matches /.*suffix$/, no hasPrefix()/hasSuffix() methods.

Keywords: endsWith, string, matches, starts, begins, text, ends, startsWith, regex, prefix, suffix