Skip to content
Draft

Strings

Strings in Draft are UTF-8 encoded and immutable.

@simple = "Hello, World!"
@empty = ""
@multiline = "Line 1
Line 2
Line 3"
@name = "World"
@greeting = "Hello, {@name}!"
@// Result: "Hello, World!"
@text = "Hello"
@text#length @// 5
@text#is_empty @// false
@text#chars @// Array of characters
@text#bytes @// Array of bytes
@"Hello"#upper() @// "HELLO"
@"Hello"#lower() @// "hello"
@"hello"#capitalize() @// "Hello"
@"hello world"#title_case() @// "Hello World"
@" hello "#trim() @// "hello"
@" hello "#trim_start() @// "hello "
@" hello "#trim_end() @// " hello"
@"Hello World"#contains("World") @// true
@"Hello World"#starts_with("Hello") @// true
@"Hello World"#ends_with("World") @// true
@"Hello World"#index_of("World") @// 6
@"Hello World"#last_index_of("l") @// 9
@"Hello World"#substring(0, 5) @// "Hello"
@"Hello World"#slice(6) @// "World"
@"a,b,c"#split(",") @// ["a", "b", "c"]
@"Hello World"#split(" ") @// ["Hello", "World"]
@["a", "b", "c"]#join(",") @// "a,b,c"
@"Hello World"#replace("World", "Draft") @// "Hello Draft"
@"aaa"#replace("a", "b") @// "bbb"
@"hello"#pad_start(10) @// " hello"
@"hello"#pad_end(10) @// "hello "
@"42"#pad_start(5, "0") @// "00042"
@"hello"#reverse() @// "olleh"
@char = 'A'
@char#is_upper() @// true
@char#is_lower() @// false
@char#is_digit() @// false
@char#is_alpha() @// true
@char#is_alphanumeric() @// true
@char#is_whitespace() @// false
@char#to_lower() @// 'a'
@char#to_upper() @// 'A'
for @char in "Hello"
write(@char)
fin for.

Strings without escape processing:

@path = r"C:\Users\Alice\Documents"
@regex = r"^\d+$"

For efficient string concatenation:

@builder = String::new()
@builder#append("Hello")
@builder#append(" ")
@builder#append("World")
@result = @builder#to_string() @// "Hello World"
@formatted = format("Hello, {0}! You are {1} years old.", "Alice", 30)
@// "Hello, Alice! You are 30 years old."
@num = "42"#to_int() @// 42
@flt = "3.14"#to_float() @// 3.14
@str = 42#to_string() @// "42"
@flt_str = 3.14#to_string() @// "3.14"
@text = """
This is a
multiline string
with preserved formatting.
"""
@"a" == @"b" @// false
@"a" != @"b" @// true
@"a" < @"b" @// true (lexicographic)
@"abc" == @"abc" @// true

Learn about Modules for code organization.