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, and String has no endsWith(). String does have startsWith(prefix), and the matches operator with regex anchors covers both a prefix and a suffix.

STARTS WITH (PREFIX)

Call startsWith, or use regex with the ^ anchor:

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

ENDS WITH (SUFFIX)

There is no endsWith(), so use regex with the $ 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. startsWith covers a fixed prefix; the matches operator with regex provides the rest (every suffix check, and complex patterns) with more flexibility.

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.startsWith("Hello")
        stdout.println("startsWith Hello")

      startsWithGoodbye <- greeting matches /^Goodbye.*/
      stdout.println(`Starts with Goodbye: ${startsWithGoodbye}`)

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

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

      endsWithEarth <- greeting matches /.*Earth$/
      stdout.println(`Ends with Earth: ${endsWithEarth}`)

      // === 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 startsWith(prefix), or the matches operator with regex anchors (^, $), for prefix checks; suffix checks use the $ anchor because there is no endsWith(). 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.startsWith(prefix), str matches /^prefix.*/, str matches /.*suffix$/, no endsWith(), hasPrefix() or hasSuffix() methods.

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