string
===============================================================================
%% A `string` represents an immutable sequence of Unicode characters. Strings are used for textual data, identifiers, labels, and human-readable values. They are first-class values and integrate naturally with records, tuples, tables, maps, and comprehensions.
1. Syntax
-------------------------------------------------------------------------------
String literals are written using double quotes `"`.
```mech:disabled
""
"Hello, world!"
"Multi
Line
String"
"Unicode: 🤖🦾🦿"
```
Strings may contain spaces and punctuation. Whitespace inside a string literal is preserved.
Escape sequences may be used for special characters:
```
"line 1\nline 2"
"quote: \""
"tab\tseparated"
"backslash: \\"
```
When rendered:
```mech:disabled
"line 1\nline 2"
"quote: \""
"tab\tseparated"
"backslash: \\"
```
Raw string literals can be created using triple double quotes `"""`. In this form, escape sequences are not processed, and all characters are taken literally, including backslashes and quotes.
```
"""C:\Users\Name\Documents"""
"""This is a "raw" \string\ literal."""
```
When rendered:
```mech:disabled
"""C:\Users\Name\Documents"""
"""This is a "raw" \string\ literal."""
```
2. Kind
-------------------------------------------------------------------------------
Strings have the kind:
```mech:disabled
<string>
```
All string values share the same kind regardless of length or contents.
3. Construction
-------------------------------------------------------------------------------
Strings can be constructed in the following ways:
- String literals
- Results of string operations
- Conversion from other kinds
(3.1) String Literals
String literals are written using double quotes `"` and may contain escape sequences and newlines:
```mech:disabled
"Hello"
"This is a string literal."
"This is a multiline
string literal"
"This string contains a newline:\nSee?"
```
Raw string literals are written with triple quotes and can contain unescaped characters, as well as newlines:
```mech:disabled
"""C:\Program Files\Mech"""
"""This is a raw string literal.It can span multiple lines
and include "quotes" and \backslashes\ without needing escapes."""
```
(3.2) String Concatenation
Strings can be concatenated using the `+` operator:
```mech:ex 3.2.1
first := "Hello"
second := "World"
greeting := first + ", " + second + "!"
```
String concatentation is broadcast element-wise over matrices of strings:
```mech:ex 3.2.2
prefix := "Item"
letters := ["A" "B" "C"]
labels := prefix + letters
```
If both operands are matrices of strings, they must have compatible dimensions:
```mech:ex 3.2.3
m1 := ["a" "b"; "c" "d"]
m2 := ["1" "2"; "3" "4"]
result := m1 + m2
```
(3.3) Conversion to String
All values can be converted to strings explicitly.
```mech:ex 3.3
x := 42
s<string> := x
```
Use {{<[string]>}} to create a matrix of strings:
```mech:ex 3.4
m<[string]:2,2> := [1 2 3 4]
```
This converts a {{<[f64]:1,4>}} (row matrix of floats) to a {{<[string]:2,2>}} (square matrix of strings).