strings

The strings module provides common string operations like searching, comparison, and transformation.

import "conjure/strings"

Search Functions

startsWith

Checks if a string starts with the given prefix.

var isHttp = strings.startsWith("https://example.com", "https")  // true
var noMatch = strings.startsWith("hello", "world")                // false

Signature:

func startsWith(s string, prefix string) bool

endsWith

Checks if a string ends with the given suffix.

var isJson = strings.endsWith("config.json", ".json")  // true
var notTxt = strings.endsWith("config.json", ".txt")   // false

Signature:

func endsWith(s string, suffix string) bool

contains

Checks if a string contains the given substring.

var hasWorld = strings.contains("hello world", "world")  // true
var hasNope  = strings.contains("hello world", "nope")   // false
var empty    = strings.contains("any string", "")        // true (empty always matches)

Signature:

func contains(s string, substr string) bool

Transformation Functions

trim

Removes whitespace from both ends of a string. This includes spaces, tabs, and newlines.

var cleaned = strings.trim("  hello world  ")  // "hello world"
var tabs    = strings.trim("\t\tdata\n")       // "data"

Signature:

func trim(s string) string

toLower

Converts all characters in a string to lowercase.

var lower = strings.toLower("Hello World")  // "hello world"
var mixed = strings.toLower("HeLLo123")     // "hello123"

Signature:

func toLower(s string) string

toUpper

Converts all characters in a string to uppercase.

var upper = strings.toUpper("Hello World")  // "HELLO WORLD"
var mixed = strings.toUpper("hello123")     // "HELLO123"

Signature:

func toUpper(s string) string

Substring Functions

substring

Extracts a portion of a string from a start index to an end index (exclusive). If the indices are out of bounds, they are clamped to valid range.

var hello = strings.substring("hello world", 0, 5)   // "hello"
var world = strings.substring("hello world", 6, 11)  // "world"

Signature:

func substring(s string, start i32, end i32) string

Returns an empty string if start >= end or start >= length.

substringFrom

Extracts a substring from the start index to the end of the string.

var rest = strings.substringFrom("hello world", 6)  // "world"
var all  = strings.substringFrom("hello", 0)        // "hello"

Signature:

func substringFrom(s string, start i32) string

Returns an empty string if start is out of bounds.

Example: Path Validation

import "conjure/strings"

func isValidImagePath(path string) bool {
    // Must not be empty
    if path.len == 0 {
        return false
    }

    // Check for valid image extensions
    if strings.endsWith(path, ".png") {
        return true
    }
    if strings.endsWith(path, ".jpg") {
        return true
    }
    if strings.endsWith(path, ".gif") {
        return true
    }

    return false
}

Example: Simple Parser

import "conjure/strings"
import "conjure/io"

func parseKeyValue(line string) {
    var trimmed = strings.trim(line)

    // Skip comments
    if strings.startsWith(trimmed, "#") {
        return
    }

    // Skip empty lines
    if trimmed.len == 0 {
        return
    }

    // Check for key=value format
    if strings.contains(trimmed, "=") {
        io.println("Found config: #{trimmed}")
    }
}