Skip to content
NorscodeNorscode

std.tekst

ReferenceBy the Norscode project

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, with lim between each part.
  • del_av(tekstverdi: tekst, start: heltall, slutt: heltall) -> tekst — Takes the slice from start up 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 of gammel with ny.
  • 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 text n times 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 between start and slutt (same as del_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 with fylltegn up to breidd.
  • fyll_venstre(tekstverdi: tekst, breidd: heltall, fylltegn: tekst) -> tekst — Pads the text on the left up to breidd.
  • senter(tekstverdi: tekst, breidd: heltall, fylltegn: tekst) -> tekst — Centres the text within breidd by padding on both sides.
  • nullfyll(verdi: heltall, breidd: heltall) -> tekst — Formats a number with leading zeros up to breidd.
  • 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/substring include the character at the start index, but not at the end index.
  • finn returns -1 when 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_saman at 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

Back to the overview