Documentation fixes. No code changes. (0.40.5)
This commit is contained in:
parent
e7e1343a83
commit
02f0c07bce
28 changed files with 271 additions and 496 deletions
|
|
@ -33,14 +33,12 @@ This guide covers string literals, common operations (length, concatenation, sub
|
|||
|
||||
## Literals and escaping
|
||||
|
||||
```
|
||||
s1 = "hello"
|
||||
<pre>s1 = "hello"
|
||||
s2 = "line1\nline2" // newline
|
||||
s3 = "quote: \" and backslash: \\" // escaped quote and backslash
|
||||
|
||||
print(s1) // hello
|
||||
```
|
||||
|
||||
</pre>
|
||||
Notes:
|
||||
- Strings are immutable; operations return new strings rather than modifying in place.
|
||||
- Use to_string(x) when concatenating non-string values.
|
||||
|
|
@ -49,66 +47,54 @@ Notes:
|
|||
|
||||
Length and concatenation:
|
||||
|
||||
```
|
||||
name = "Ada"
|
||||
<pre>name = "Ada"
|
||||
greet = "Hello, " + name + "!" // "Hello, Ada!"
|
||||
print(len(greet)) // 12
|
||||
```
|
||||
|
||||
</pre>
|
||||
Substring (start, length) and search:
|
||||
|
||||
```
|
||||
s = "hello, world"
|
||||
<pre>s = "hello, world"
|
||||
print(substr(s, 7, 5)) // world
|
||||
idx = find(s, ",") // 5, or -1 if not found
|
||||
if idx >= 0 { print("comma at index " + to_string(idx)) }
|
||||
```
|
||||
|
||||
</pre>
|
||||
Splitting into arrays:
|
||||
|
||||
```
|
||||
parts = split("a,b,c", ",") // ["a","b","c"]
|
||||
<pre>parts = split("a,b,c", ",") // ["a","b","c"]
|
||||
for i = 0; i < len(parts); i = i + 1 {
|
||||
print(parts[i])
|
||||
}
|
||||
```
|
||||
|
||||
</pre>
|
||||
## Conversions and formatting
|
||||
|
||||
```
|
||||
n = 42
|
||||
<pre>n = 42
|
||||
pi = 3.14
|
||||
msg = "n=" + to_string(n) + ", pi=" + to_string(pi)
|
||||
print(msg)
|
||||
|
||||
// parsing (may error if the string is not numeric)
|
||||
n2 = to_number("123") // 123
|
||||
```
|
||||
|
||||
</pre>
|
||||
If you need a specific type, you can use cast for advanced cases, e.g. cast("123", "number").
|
||||
|
||||
## Common patterns
|
||||
|
||||
- Guard on find results before slicing:
|
||||
|
||||
```
|
||||
email = "user@example.org"
|
||||
<pre>email = "user@example.org"
|
||||
at = find(email, "@")
|
||||
if at >= 0 {
|
||||
user = substr(email, 0, at)
|
||||
host = substr(email, at + 1, len(email) - at - 1)
|
||||
print(user + " on " + host)
|
||||
}
|
||||
```
|
||||
|
||||
</pre>
|
||||
- Building paths or messages:
|
||||
|
||||
```
|
||||
base = "/tmp"
|
||||
<pre>base = "/tmp"
|
||||
file = "log.txt"
|
||||
path = base + "/" + file
|
||||
```
|
||||
|
||||
</pre>
|
||||
## Gotchas
|
||||
|
||||
- Strings are immutable: repeated concatenation in big loops can be costly; consider collecting pieces in an array and joining at the end if you have a helper for that in your setup.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue