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
- Write a function
count_char(s: String, target: String) -> intthat counts how many times a single-character string appears ins. Hint: use.split()— the result length minus 1 gives the count.
- Write a function
is_palindrome(s: String) -> boolthat returnstrueif a string reads the same forwards and backwards. Hint: compare the string with its reverse.
- Write a function
capitalize_first(s: String) -> Stringthat 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.
- Write a program that takes a sentence and prints each word on its own line, numbered:
`` 1. Nyx 2. is 3. great ``
- Write a function
replace_all(text: String, old: String, new_val: String) -> Stringthat replaces all occurrences ofoldwithnew_val. Hint: split byold, then join withnew_val.
Summary
- Strings are text enclosed in double quotes:
"Hello" +joins strings together (concatenation)..length()returns the number of characters..charAt(i)returns the character at positioni(as ASCII integer)..substring(start, end)extracts a portion..indexOf(s)finds the position of a substring (-1 if not found)..split(sep)divides a string into an array.int_to_string()andstring_to_int()convert between types.char_to_string()converts a character to a single-character string.
Next chapter: Maps →