string_editor
Values
pub fn after(
string: String,
on pattern: String,
) -> Result(String, Nil)
Returns the part of a string after the first occurrence of a given substring.
If the substring is not found, or is empty, it returns Error(Nil).
Examples
after(“hello world”, on: “ “) Ok(“world”)
after(“gleam is fun”, on: “!”) Error(Nil)
pub fn after_all(
string: String,
on pattern: String,
) -> List(String)
Returns all parts of a string after each occurrence of a given substring.
Examples
after_all(“a.b.c.d”, on: “.”) [“b.c.d”, “c.d”, “d”]
after_all(“hello world”, on: “!”) []
pub fn after_at(
string: String,
on pattern: String,
at index: Int,
) -> Result(String, Nil)
Returns the part of a string after the nth occurrence of a given substring.
Index is 0-based. If the pattern doesn’t occur enough times, returns Error(Nil).
Examples
after_at(“a.b.c.d”, on: “.”, at: 1) Ok(“c.d”)
after_at(“hello world”, on: “ “, at: 5) Error(Nil)
pub fn after_last(
string: String,
on pattern: String,
) -> Result(String, Nil)
Returns the part of a string after the last occurrence of a given substring.
If the substring is not found, or is empty, it returns Error(Nil).
Examples
after_last(“/home/user/document.txt”, on: “/”) Ok(“document.txt”)
after_last(“gleam is fun”, on: “!”) Error(Nil)
pub fn before(
string: String,
on pattern: String,
) -> Result(String, Nil)
Returns the part of a string before the first occurrence of a given substring.
If the substring is not found, or is empty, it returns Error(Nil).
Examples
before(“hello world”, on: “ “) Ok(“hello”)
before(“gleam is fun”, on: “!”) Error(Nil)
pub fn before_all(
string: String,
on pattern: String,
) -> List(String)
Returns all parts of a string before each occurrence of a given substring.
Examples
before_all(“a.b.c.d”, on: “.”) [“a”, “a.b”, “a.b.c”]
before_all(“hello world”, on: “!”) []
pub fn before_at(
string: String,
on pattern: String,
at index: Int,
) -> Result(String, Nil)
Returns the part of a string before the nth occurrence of a given substring.
Index is 0-based. If the pattern doesn’t occur enough times, returns Error(Nil).
Examples
before_at(“a.b.c.d”, on: “.”, at: 1) Ok(“a.b”)
before_at(“hello world”, on: “ “, at: 5) Error(Nil)
pub fn before_last(
string: String,
on pattern: String,
) -> Result(String, Nil)
Returns the part of a string before the last occurrence of a given substring.
If the substring is not found, or is empty, it returns Error(Nil).
Examples
before_last(“/home/user/document.txt”, on: “/”) Ok(“/home/user”)
before_last(“gleam is fun”, on: “!”) Error(Nil)
pub fn between(
string: String,
from start: String,
to end: String,
) -> Result(String, Nil)
Returns the part of a string between two given substrings.
It finds the first occurrence of start and then the first
occurrence of end after start. If either is not found in the
correct order, or is empty, it returns Error(Nil).
Examples
between(“
title
”, from: “”, to: “”) Error(Nil)
pub fn between_all(
string: String,
from start: String,
to end: String,
) -> List(String)
pub fn between_at(
string: String,
from start: String,
to end: String,
at index: Int,
) -> Result(String, Nil)
Returns the part of a string between the nth occurrence of start and the first occurrence of end after that.
Index is 0-based for the start pattern. If patterns don’t occur enough times, returns Error(Nil).
Examples
between_at(“
title
”, from: “”, to: “”, at: 0) Error(Nil)
pub fn count(string: String, of pattern: String) -> Int
Counts the number of non-overlapping occurrences of a substring in a
string. An empty pattern always counts as 0.
Examples
count(“hello hello world”, of: “hello”) 2
count(“gleam is fun”, of: “rust”) 0
count(“aaaa”, of: “aa”) 2
pub fn replace_after(
string: String,
on pattern: String,
with replacement: String,
) -> Result(String, Nil)
Replaces the part of a string after the first occurrence of a given substring, keeping the pattern itself.
If the substring is not found, or is empty, it returns Error(Nil).
Examples
replace_after(“hello world”, on: “ “, with: “gleam”) Ok(“hello gleam”)
replace_after(“gleam is fun”, on: “!”, with: “x”) Error(Nil)
pub fn replace_before(
string: String,
on pattern: String,
with replacement: String,
) -> Result(String, Nil)
Replaces the part of a string before the first occurrence of a given substring, keeping the pattern itself.
If the substring is not found, or is empty, it returns Error(Nil).
Examples
replace_before(“hello world”, on: “ “, with: “goodbye”) Ok(“goodbye world”)
replace_before(“gleam is fun”, on: “!”, with: “x”) Error(Nil)
pub fn replace_between(
string: String,
from start: String,
to end: String,
with replacement: String,
) -> Result(String, Nil)
Replaces the part of a string between two given substrings, keeping both delimiters.
It finds the first occurrence of start and then the first
occurrence of end after start. If either is not found in the
correct order, or is empty, it returns Error(Nil).
Examples
replace_between(“old”, from: “”, to: “”, with: “new”) Ok(“new”)
replace_between(“
title
”, from: “”, to: “”, with: “x”) Error(Nil)