string
API summary (generated from the Beans source by npm run coverage): 28 instance methods.
string is immutable UTF-8 text. Once you make a string, it never changes. A
method that “changes” a string really returns a new string.
Strings are byte-based. len() returns the number of bytes, not characters, and
this never changes. Indexes into a string are byte positions.
The string type is a native builtin, reached through the runtime ABI table at
compiler/beans/expression.b.
Making and joining strings
Section titled “Making and joining strings”There is no + on strings. To build a string from parts, use interpolation:
let name: string = "beans"let greeting: string = "hi {name}"To join a List<string> with a separator, use List.join. See
Collections.
Size and emptiness
Section titled “Size and emptiness”string.len() -> intstring.is_empty() -> boollen()is the number of bytes, not characters.is_empty()is true whenlen()is 0.
Taking pieces
Section titled “Taking pieces”string.first(int) -> stringstring.last(int) -> stringstring.slice(int, int) -> stringstring.byte_at(int) -> intfirst(n)returns the firstnbytes;last(n)returns the lastnbytes.slice(from, to)returns the half-open byte range[from, to). It panics if the range is out of bounds.byte_at(i)returns the byte value at indexi, and panics ifiis out of range.
let s: string = "hello"let h: string = s.first(1)let lo: string = s.slice(3, 5)Searching
Section titled “Searching”string.contains(string) -> boolstring.starts_with(string) -> boolstring.ends_with(string) -> boolstring.find(string) -> Option<int>string.rfind(string) -> Option<int>contains,starts_with, andends_withanswer a yes/no question.findreturns the byte index of the first match,rfindthe last. Both returnnonewhen the needle is not present.- For an empty needle,
findreturns0andrfindreturnslen.
match "hello".find("ll") { some(i) => io.println("found at {i}"), none => io.println("not found"),}Cleaning and case
Section titled “Cleaning and case”string.trim() -> stringstring.trim_start() -> stringstring.trim_end() -> stringstring.to_upper() -> stringstring.to_lower() -> stringtrimremoves ASCII whitespace from both ends;trim_startandtrim_endremove it from one end.to_upperandto_lowerchange ASCII letters only. Non-ASCII bytes are left as they are.
Building new strings
Section titled “Building new strings”string.replace(string, string) -> stringstring.repeat(int) -> stringreplace(old, new)replaces every match ofold. An emptyoldchanges nothing.repeat(n)repeats the stringntimes, and panics on a negativen.
Splitting
Section titled “Splitting”string.split(string) -> List<string>string.lines() -> List<string>split(sep)splits onsepand keeps empty pieces. An emptysepreturns the whole string as one piece.lines()splits the string into lines.
let parts: List<string> = "a,b,,c".split(",")// ["a", "b", "", "c"]Parsing to numbers
Section titled “Parsing to numbers”string.to_int() -> Result<int>string.to_float() -> Result<float>string.to_decimal() -> Result<decimal>Each returns a Result because the text may not be a number. See
Option, Result, and Error.
Characters (UTF-8)
Section titled “Characters (UTF-8)”len() counts bytes. These two methods work with whole UTF-8 characters.
string.chars() -> List<string>string.count_chars(int, int) -> intchars()returns each UTF-8 character as its own one-character string.count_chars(from, to)returns the number of characters in the byte range[from, to).
Low-level helpers
Section titled “Low-level helpers”These work directly on bytes and use plain return values, not Option.
string.find_byte(int, int) -> intstring.range_equals(int, int, string) -> boolstring.parse_int_range_or(int, int, int) -> intfind_byte(byte, from)returns the index ofbyteat or afterfrom, or-1when it is absent.range_equals(from, to, other)is true when the byte range[from, to)equalsother.parse_int_range_or(from, to, fallback)parses the byte range as an int, or returnsfallbackwhen it is not a number.
A short tour
Section titled “A short tour”import std.io
fn main() { let s: string = " Beans Language " let t: string = s.trim() io.println("{t.len()} bytes, empty {t.is_empty()}") io.println("{t.to_upper()} / {t.to_lower()}") io.println("{t.starts_with("Beans")} {t.contains("Lang")}")
match t.find("Lang") { some(i) => io.println("Lang at byte {i}"), none => io.println("not found"), }
let parts: List<string> = "a,b,,c".split(",") io.println("{parts.len()} parts, third is \"{parts[2]}\"")
let n: int = " 42 ".trim().to_int().expect("a number") io.println("{n + 1}")}See also
Section titled “See also”- Bytes, a changeable byte buffer.
- Numbers and decimal, the parse targets.
- Collections,
List<string>andjoin.