Table of Contents

Strings

What is a string?

A string is a piece of text. Every time you write something between double quotes in Nyx, you create a string:

fn main() {
    let greeting: String = "Hello, World!"
    print(greeting)
}

The name "string" comes from the idea that text is a "string of characters" — like beads on a necklace, each character is one bead.

Creating strings

fn main() {
    let name: String = "Alice"
    let empty: String = ""
    let sentence: String = "Nyx is a compiled language."

    print(name)
    print(empty)        // (prints nothing)
    print(sentence)
}

Joining strings — concatenation

You can join two strings together using the + operator. This is called concatenation:

fn main() {
    let first: String = "Hello"
    let second: String = "World"
    let result: String = first + ", " + second + "!"

    print(result)    // Hello, World!
}

You can also concatenate strings with numbers by converting them first:

fn main() {
    let age: int = 25
    let message: String = "I am " + int_to_string(age) + " years old."
    print(message)    // I am 25 years old.
}

int_to_string() converts an integer to its text representation. This is necessary because + with strings means "join text", not "add numbers".

String length

The .length() method tells you how many characters a string has:

fn main() {
    let word: String = "Nyx"
    print(word.length())    // 3

    let empty: String = ""
    print(empty.length())    // 0

    let phrase: String = "Hello, World!"
    print(phrase.length())   // 13
}

Accessing individual characters

You can access a single character using .charAt():

fn main() {
    let word: String = "Hello"

    let first: char = word.charAt(0)    // 'H'
    let last: char = word.charAt(4)     // 'o'

    // Compare characters using ASCII values
    if first == 72 {    // 72 is ASCII for 'H'
        print("Starts with H")
    }
}

Note: .charAt() returns an integer (the ASCII value of the character). To compare, use the ASCII value directly.

Substrings

The .substring(start, end) method extracts a portion of a string. It returns the characters from position start up to (but not including) position end:

fn main() {
    let text: String = "Hello, World!"

    let hello: String = text.substring(0, 5)
    print(hello)    // Hello

    let world: String = text.substring(7, 12)
    print(world)    // World
}

Searching in strings

indexOf

The .indexOf() method finds the position of a substring within a string. It returns -1 if not found:

fn main() {
    let text: String = "The quick brown fox"

    let pos: int = text.indexOf("quick")
    print(pos)     // 4

    let not_found: int = text.indexOf("slow")
    print(not_found)    // -1
}

startsWith and endsWith

fn main() {
    let filename: String = "report.pdf"

    if filename.endsWith(".pdf") {
        print("It's a PDF file")
    }

    let url: String = "https://nyxlang.com"
    if url.startsWith("https") {
        print("Secure connection")
    }
}

Splitting strings

The .split() method divides a string into an array based on a separator:

fn main() {
    let csv: String = "Alice,Bob,Charlie,Diana"
    let names: Array = csv.split(",")

    var i: int = 0
    while i < names.length() {
        print(names[i])
        i += 1
    }
}

Output:

Alice
Bob
Charlie
Diana

Converting between types

fn main() {
    // Integer to string
    let n: int = 42
    let s: String = int_to_string(n)
    print(s)    // "42"

    // String to integer
    let text: String = "123"
    let num: int = string_to_int(text)
    print(num + 1)    // 124
}

Practical example: counting words

fn count_words(text: String) -> int {
    let words: Array = text.split(" ")
    return words.length()
}

fn main() {
    let sentence: String = "Nyx is a fast compiled language"
    print(count_words(sentence))    // 6
}

Practical example: reversing a string

fn reverse_string(s: String) -> String {
    var result: String = ""
    var i: int = s.length() - 1
    while i >= 0 {
        let c: char = s.charAt(i)
        result = result + char_to_string(c)
        i -= 1
    }
    return result
}

fn main() {
    print(reverse_string("hello"))    // olleh
    print(reverse_string("Nyx"))      // xyN
}

Practical example: building strings from parts

fn repeat_string(s: String, times: int) -> String {
    var result: String = ""
    var i: int = 0
    while i < times {
        result = result + s
        i += 1
    }
    return result
}

fn main() {
    print(repeat_string("ha", 3))     // hahaha
    print(repeat_string("=-", 10))    // =-=-=-=-=-=-=-=-=-=-
}

Exercises

  1. Write a function count_char(s: String, target: String) -> int that counts how many times a single-character string appears in s. Hint: use .split() — the result length minus 1 gives the count.
  1. Write a function is_palindrome(s: String) -> bool that returns true if a string reads the same forwards and backwards. Hint: compare the string with its reverse.
  1. Write a function capitalize_first(s: String) -> String that returns the string with its first character in uppercase. Assume the input starts with a lowercase letter. Hint: extract the first character, convert manually, and concatenate with the rest.
  1. Write a program that takes a sentence and prints each word on its own line, numbered:

`` 1. Nyx 2. is 3. great ``

  1. Write a function replace_all(text: String, old: String, new_val: String) -> String that replaces all occurrences of old with new_val. Hint: split by old, then join with new_val.

Summary

Next chapter: Maps →

← Previous: Arrays Next: Maps →