2026-04-10 23:27:55 +02:00
---
layout: page
published: true
2026-04-11 00:18:36 +02:00
noToc: false
2026-04-10 23:27:55 +02:00
noComments: false
noDate: false
title: Fun - Working with strings in Fun
subtitle: Working with strings, literals/escaping, concatenation, substr/find, split, and conversions.
description: Working with strings, literals/escaping, concatenation, substr/find, split, and conversions.
permalink: /documentation/strings/
lang: en
tags:
2026-04-11 00:18:36 +02:00
- concatenation
- conversions
- escaping
- find
- literals
- split
- strings
- substr
2026-04-10 23:27:55 +02:00
---
2026-02-17 14:59:39 +01:00
This guide covers string literals, common operations (length, concatenation, substring, search, split), and interop patterns. Strings in Fun are immutable sequences of bytes/text.
## TL;DR
- Create with quotes: s = "hello". Escape with \n, \t, \", \\.
- Concatenate with +. Convert non-strings with to_string(x).
- Length: len(s). Substring: substr(s, start, length).
- Find index: find(s, needle) → 0-based index or -1 if not found.
- Split CSV: split("a,b,c", ",") → ["a","b","c"].
## Literals and escaping
2026-04-11 02:38:47 +02:00
<pre>s1 = "hello"
2026-02-17 14:59:39 +01:00
s2 = "line1\nline2" // newline
s3 = "quote: \" and backslash: \\" // escaped quote and backslash
2026-04-15 01:25:17 +02:00
print(s1) // hello</pre>
2026-02-17 14:59:39 +01:00
Notes:
2026-04-15 01:25:17 +02:00
2026-02-17 14:59:39 +01:00
- Strings are immutable; operations return new strings rather than modifying in place.
- Use to_string(x) when concatenating non-string values.
## Basic operations
Length and concatenation:
2026-04-11 02:38:47 +02:00
<pre>name = "Ada"
2026-02-17 14:59:39 +01:00
greet = "Hello, " + name + "!" // "Hello, Ada!"
2026-04-15 01:25:17 +02:00
print(len(greet)) // 12</pre>
2026-02-17 14:59:39 +01:00
Substring (start, length) and search:
2026-04-11 02:38:47 +02:00
<pre>s = "hello, world"
2026-02-17 14:59:39 +01:00
print(substr(s, 7, 5)) // world
idx = find(s, ",") // 5, or -1 if not found
2026-04-15 01:25:17 +02:00
if idx >= 0 { print("comma at index " + to_string(idx)) }</pre>
2026-02-17 14:59:39 +01:00
Splitting into arrays:
2026-04-11 02:38:47 +02:00
<pre>parts = split("a,b,c", ",") // ["a","b","c"]
2026-02-17 14:59:39 +01:00
for i = 0; i < len(parts); i = i + 1 {
print(parts[i])
2026-04-15 01:25:17 +02:00
}</pre>
2026-02-17 14:59:39 +01:00
## Conversions and formatting
2026-04-11 02:38:47 +02:00
<pre>n = 42
2026-02-17 14:59:39 +01:00
pi = 3.14
msg = "n=" + to_string(n) + ", pi=" + to_string(pi)
print(msg)
// parsing (may error if the string is not numeric)
2026-04-15 01:25:17 +02:00
n2 = to_number("123") // 123</pre>
2026-02-17 14:59:39 +01:00
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:
2026-04-11 02:38:47 +02:00
<pre>email = "user@example .org"
2026-02-17 14:59:39 +01:00
at = find(email, "@")
if at >= 0 {
user = substr(email, 0, at)
host = substr(email, at + 1, len(email) - at - 1)
print(user + " on " + host)
2026-04-15 01:25:17 +02:00
}</pre>
2026-02-17 14:59:39 +01:00
- Building paths or messages:
2026-04-11 02:38:47 +02:00
<pre>base = "/tmp"
2026-02-17 14:59:39 +01:00
file = "log.txt"
2026-04-15 01:25:17 +02:00
path = base + "/" + file</pre>
2026-02-17 14:59:39 +01:00
## 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.
2026-04-15 01:25:17 +02:00
- len(s) counts bytes/code units; be mindful when working with multibyte encodings.
2026-02-17 14:59:39 +01:00
## See also
2026-04-15 01:25:17 +02:00
- Core overview: [../types/ ](../types/ )
- Arrays guide (useful when splitting/collecting text): [../arrays/ ](../arrays/ )