Skip to content

Search and Test

Three methods cover most format checks and filtering without needing regular expressions:

  • .contains(sub) — does the substring exist at any position?
  • .starts_with(prefix) — does it begin with that text?
  • .ends_with(suffix) — does it end with that text?

All three are case-sensitive: normalize with .lower() first if you need a case-insensitive comparison.

Prefix, suffix, and substring checks; URL and file-extension validation.

06-search-and-test.zolo
Playground
// Feature: Searching and testing strings

// Syntax: `.contains(sub)`, `.starts_with(p)`, `.ends_with(s)`

// When to use: validate format, filter, decide without regex.


use std::regex
use std::url

let phrase = "the quick brown fox"

// -- contains: does the substring exist? ------------------------

print(phrase.contains("quick"))
// expected: true

print(phrase.contains("cat"))

// expected: false


// -- starts_with / ends_with -----------------------------------

print(phrase.starts_with("the"))
// expected: true

print(phrase.ends_with("fox"))
// expected: true

print(phrase.starts_with("THE"))

// expected: false (case-sensitive)


// -- Practical cases -------------------------------------------


// Validate URL prefix.

fn is_https(url: str) -> bool {
  return url.starts_with("https://")
}

print(is_https("https://zolo-lang.devzolo.com"))  // true

print(is_https("http://example.com"))  // false


// Validate file extension.

fn is_image(name: str) -> bool {
  return name.ends_with(".png") || name.ends_with(".jpg")
}

print(is_image("photo.png"))  // true

print(is_image("doc.pdf"))  // false


// Filter list by substring.

let files = ["main.zolo", "test.rs", "lib.zolo", "build.sh"]
for f in files {
  if f.contains(".zolo") {
    print(f)
  }
}
// expected:

// main.zolo

// lib.zolo

Challenge

Write a function is_zolo_file(name: str) -> bool that returns true only if the name ends with .zolo and does not start with _. Test with "main.zolo", "_test.zolo", and "lib.rs".

enespt-br