Skip to content

Strings

ffredyk edited this page Jul 23, 2026 · 1 revision

Strings & Text

SQ# strings are immutable .NET strings with modern enhancements: escape sequences, verbatim strings, multi-line literals, interpolation, and a rich set of string commands.

String Literals

Double-Quoted (Escape Sequences)

_name = "Hello\nWorld";        // \n = newline
_tab = "Col1\tCol2";           // \t = tab
_quote = "He said \"Hi\"";     // \" = escaped quote
_path = "C:\\Users\\Name";     // \\ = backslash
_unicode = "\u00A9";           // © (Unicode escape)

Supported escapes: \n, \r, \t, \\, \", \', \uXXXX

Single-Quoted (Minimal Escapes)

_path = 'C:\Users\Name';       // backslashes are literal
_quote = 'It\'s fine';         // only ' needs escaping

Verbatim Strings

_path = @"C:\Users\Name\Documents";  // NO escapes at all
_regex = @"\d+\.\d+";                // great for regex patterns

Multi-Line Strings

_help = """
    Usage: mycommand [options]
    
    Options:
      -h, --help     Show this message
      -v, --verbose  Enable verbose output
    """;

Leading/trailing whitespace on the first/last line is trimmed.

String Interpolation (f-strings)

_name = "Fred";
_hp = 75;
_maxHp = 100;

// Basic interpolation
_msg = f"Player {_name} has {_hp} HP";

// Expressions in braces
_msg = f"HP: {_hp}/{_maxHp} ({_hp / _maxHp * 100}%)";

// Multi-line interpolation
_report = f"""
    Name: {_name}
    HP: {_hp}/{_maxHp}
    Status: {if (_hp > 50) then {"Healthy"} else {"Wounded"}}
    """;

f-strings are compile-time — the template is parsed and compiled once. Prefer f-strings over format when the template is known at compile time.

format — Runtime Formatting

Use when the template is dynamic (loaded from config, user input):

// %1, %2, ... placeholders
_result = format ["HP: %1/%2", _hp, _maxHp];  // "HP: 75/100"

// Dynamic template
_template = loadConfig("messageTemplate");
_msg = format [_template, _name, _score];

String Commands Quick Reference

Command Description Example
count String length count "hello"5
select Character at index "hello" select 1"e"
find Substring index (-1 if not found) "hello" find "ll"2
in Contains check "ell" in "hello"true
+ Concatenation "Hello " + "World""Hello World"
str Any → string str 42"42"
format sprintf-style format ["%1 %2", "a", "b"]
parseNumber String → number (nil on fail) parseNumber "123"123
toArray String → code point array toArray "AB"[65, 66]
toString Code points → string toString [65, 66]"AB"
splitString Split by separator "a,b,c" splitString ","["a","b","c"]
joinString Join with separator joinString [["a","b"], "-"]"a-b"
toLower Lowercase toLower "HELLO""hello"
toUpper Uppercase toUpper "hello""HELLO"
trim Remove whitespace trim " hi ""hi"

Common Patterns

Case-Insensitive Comparison

_equalsIgnoreCase = {
    params ["_a", "_b"];
    toLower _a == toLower _b
};

CSV Parsing

_line = "John,Doe,42,Engineer";
_fields = _line splitString ",";
_name = _fields select 0;      // "John"
_age = parseNumber (_fields select 2);  // 42

Path Manipulation

_path = "scripts\ai\combat.sqf";
_parts = _path splitString "\";
_filename = _parts select (count _parts - 1);  // "combat.sqf"
_dir = joinString [_parts select [0, count _parts - 1], "\"];

Input Validation

_name = trim _userInput;
if (_name == "") then {
    print "Name cannot be empty";
};

_isNumeric = !isNil parseNumber _input;

Performance

// SLOW — each + creates new string (1000 allocations):
_msg = "";
for "_i" from 0 to 999 do { _msg = _msg + "x"; };

// FAST — build array, join once (1 allocation):
_parts = [];
for "_i" from 0 to 999 do { _parts pushBack "x"; };
_msg = joinString [_parts, ""];

See Also

SQ# Wiki

Home

Engine Docs

Migration

Commands

Value Constructors

Arithmetic

Comparison

Logic

Array

String

Math

Random

Type & Introspection

HashMap

Code Execution

Concurrency

Scheduler

Thread Safety

Error

Output

Time

Multiplayer

Compiler

Clone this wiki locally