std.tekst
std.tekst is the toolbox for everything to do with text — searching and checking, splitting and joining, trimming whitespace, changing case, and converting to and from numbers.
std.tekst is the toolbox for everything to do with text. Here you find the functions you use most often when handling what the user types, building text for display, or interpreting data that arrives as text: search and check, split and join, trim whitespace, change between upper and lower case, and convert to and from numbers.
The functions are small and predictable — each does one clear thing and returns a new result instead of changing the text you passed in. That makes them safe to combine: you can trim, split and convert in a chain with no side effects. Import the module with bruk std.tekst som t and call the functions with the t. prefix.
When to use it
Use std.tekst almost every time you handle user input or build text for display: checking whether something starts or ends with a pattern, splitting a line, joining parts, or clearing away whitespace. Because each function returns a new result instead of changing the original, you can safely chain them together.
Getting started
bruk std.tekst som t
funksjon start() -> heiltall {
la rå = " Ada Lovelace "
la navn = t.trim(rå) // "Ada Lovelace"
hvis t.starter_med(navn, "Ada") {
skriv(t.til_store_bokstavar(navn)) // "ADA LOVELACE"
}
la deler = t.splitt(navn, " ") // ["Ada", "Lovelace"]
skriv(deler[0]) // "Ada"
returner 0
}Note that each function returns a new result: trim does not change rå, but gives a new text. This lets you build a chain of small, predictable steps.
The functions
The functions fall into a few natural families. Some check something about the text and return true/false (starter_med, slutter_med, inneholder, er_tom, er_heltall). Others split and join (splitt, teikn, sett_saman, del_av). A third group changes the text and returns a new one (trim, erstatt, til_store_bokstavar, snu, gjenta). Then there is alignment for tidy output (fyll_venstre, fyll_høgre, senter, nullfyll) and conversion to and from numbers (tall_til_tekst, til_heltall, tegn_kode).
hilsen(navn: tekst) -> tekst— Makes a simple greeting based on the name.rop(melding: tekst) -> tekst— Turns the message into a "shouted" variant.omgitt_av_klammer(tekstverdi: tekst) -> tekst— Surrounds the text with braces.ja_nei(verdi: bool) -> tekst— Returns "ja" or "nei" from a boolean value.tall_til_tekst(x: heltall) -> tekst— Turns an integer into text.bool_til_tekst(x: bool) -> tekst— Turns a boolean value into text.starter_med(tekstverdi: tekst, prefiks: tekst) -> bool— True if the text begins with the prefix.slutter_med(tekstverdi: tekst, suffiks: tekst) -> bool— True if the text ends with the suffix.inneholder(tekstverdi, deltekst: tekst) -> bool— True if the substring occurs somewhere in the text.trim(tekstverdi: tekst) -> tekst— Removes whitespace at the start and end, returning a new text.splitt(tekstverdi: tekst, skilletegn: tekst) -> liste_tekst— Splits the text on each separator and returns a list of the parts.teikn(tekstverdi: tekst) -> liste_tekst— Returns a list of each individual character in the text.sett_saman(deler: liste_tekst, lim: tekst) -> tekst— Joins a list of texts into one, withlimbetween each part.del_av(tekstverdi: tekst, start: heltall, slutt: heltall) -> tekst— Takes the slice fromstartup to (but not including)slutt. Indexes start at 0.finn(tekstverdi: tekst, deltekst: tekst) -> heltall— Returns the index where the substring first appears, or -1 if it is not found.erstatt(tekstverdi: tekst, gammel: tekst, ny: tekst) -> tekst— Replaces all occurrences ofgammelwithny.til_store_bokstavar(tekstverdi: tekst) -> tekst— Returns the text with all letters in UPPER case.til_små_bokstavar(tekstverdi: tekst) -> tekst— Returns the text with all letters in lower case.tegn_kode(tegn: tekst) -> heltall— Returns the character code (Unicode code point) of a single character.lengde(tekstverdi: tekst) -> heltall— Number of characters in the text.gjenta(tekstverdi: tekst, n: heltall) -> tekst— Repeats the textntimes in a row.tell_forekomster(tekstverdi: tekst, deltekst: tekst) -> heltall— Counts how many times the substring occurs.substring(tekstverdi: tekst, start: heltall, slutt: heltall) -> tekst— Takes the slice betweenstartandslutt(same asdel_av).er_tom(tekstverdi: tekst) -> bool— True if the text is empty.til_heltall(tekstverdi: tekst) -> heltall— Interprets the text as an integer.fyll_høgre(tekstverdi: tekst, breidd: heltall, fylltegn: tekst) -> tekst— Pads the text on the right withfylltegnup tobreidd.fyll_venstre(tekstverdi: tekst, breidd: heltall, fylltegn: tekst) -> tekst— Pads the text on the left up tobreidd.senter(tekstverdi: tekst, breidd: heltall, fylltegn: tekst) -> tekst— Centres the text withinbreiddby padding on both sides.nullfyll(verdi: heltall, breidd: heltall) -> tekst— Formats a number with leading zeros up tobreidd.fjern_prefiks(tekstverdi: tekst, prefiks: tekst) -> tekst— Removes the prefix if the text begins with it, otherwise unchanged.fjern_suffiks(tekstverdi: tekst, suffiks: tekst) -> tekst— Removes the suffix if the text ends with it, otherwise unchanged.snu(tekstverdi: tekst) -> tekst— Returns the text reversed.er_heltall(tekstverdi: tekst) -> bool— True if the whole text is a valid integer.
Good to know
- Indexes start at 0, and
del_av/substringinclude the character at the start index, but not at the end index. finnreturns-1when the substring is not found — check for that before using the result as an index.- To build a long text in a loop, collect the parts in a list and use
sett_samanat the end; it is tidier than appending piece by piece.
std.tekst is one of the modules in the standard library. Everything ships with the runtime — no installation, no external dependencies. See also Documentation for the language and the runtime.
Related
- std.httpstd.http is a client for talking to other services over HTTP and HTTPS. You can fetch data, send data, and interpret the response — including JSON — without building the request by hand.
- std.dbstd.db gives you access to databases with a small, familiar set of functions: open a connection, run SQL, fetch values and rows, and control transactions.
- std.jsonstd.json lets your program speak JSON — the format nearly every web API uses to exchange data. parse turns JSON text into a map; stringify goes the other way.