SPECTRALANG_

// DOCS NAVIGATION

std.string — Manipulação de Strings_

SOURCE: content/docs/reference/05-stdlib.md05 Stdlib / 2. std.string — Manipulação de Strings / String Manipulation

import std.string
// ou / or
from std.string import len, trim, contains

Funções / Functions

len(s: string) -> int

PT-BR: Retorna o número de bytes da string (não necessariamente caracteres Unicode).
EN-US: Returns the number of bytes in the string (not necessarily Unicode characters).

let n = std.string.len("hello")
        // 5
let n2 = std.string.len("")
            // 0
let n3 = std.string.len("olá")
         // pode variar com Unicode

contains(s: string, sub: string) -> bool

PT-BR: Verifica se a string contém a substring.
EN-US: Checks whether the string contains the substring.

let tem = std.string.contains("hello world", "world")
  // true
let nao = std.string.contains("hello", "xyz")
          // false

to_upper(s: string) -> string

PT-BR: Converte todos os caracteres ASCII para maiúsculo.
EN-US: Converts all ASCII characters to uppercase.

let upper = std.string.to_upper("hello")
   // "HELLO"
let mixed = std.string.to_upper("Hello!")
  // "HELLO!"

to_lower(s: string) -> string

PT-BR: Converte todos os caracteres ASCII para minúsculo.
EN-US: Converts all ASCII characters to lowercase.

let lower = std.string.to_lower("WORLD")
   // "world"

trim(s: string) -> string

PT-BR: Remove espaços em branco do início e fim da string.
EN-US: Removes whitespace from the beginning and end of the string.

let limpa = std.string.trim("  hello  ")
   // "hello"
let s2 = std.string.trim("\t texto \n")
    // "texto"

starts_with(s: string, prefix: string) -> bool

let sw = std.string.starts_with("hello world", "hello")
  // true
let nao = std.string.starts_with("world", "hello")
       // false

ends_with(s: string, suffix: string) -> bool

let ew = std.string.ends_with("hello.spectra", ".spectra")
  // true

concat(a: string, b: string) -> string

PT-BR: Concatena duas strings.
EN-US: Concatenates two strings.

let ab = std.string.concat("foo", "bar")
    // "foobar"
// Nota: o operador + também concatena strings / Note: the + operator also concatenates strings
let ab2 = "foo" + "bar"
    // "foobar"

repeat_str(s: string, n: int) -> string

PT-BR: Repete a string n vezes.
EN-US: Repeats the string n times.

let rep = std.string.repeat_str("ab", 3)
    // "ababab"
let linha = std.string.repeat_str("-", 40)
  // "----------------------------------------"

char_at(s: string, index: int) -> int

PT-BR: Retorna o código Unicode do caractere na posição index. Retorna -1 se o índice estiver fora dos limites.
EN-US: Returns the Unicode code point of the character at position index. Returns -1 if the index is out of bounds.

let c = std.string.char_at("hello", 0)
     // 104 ('h')
let e = std.string.char_at("hello", 1)
     // 101 ('e')
let oob = std.string.char_at("hi", 10)
     // -1

substring(s: string, start: int, end: int) -> string

PT-BR: Extrai a substring de start até end (exclusivo).
EN-US: Extracts substring from start to end (exclusive).

let sub = std.string.substring("hello world", 0, 5)
    // "hello"
let sub2 = std.string.substring("hello world", 6, 11)
  // "world"

replace(s: string, from: string, to: string) -> string

PT-BR: Substitui todas as ocorrências de from por to.
EN-US: Replaces all occurrences of from with to.

let r = std.string.replace("hello world", "world", "SpectraLang")
// "hello SpectraLang"

index_of(s: string, sub: string) -> int

PT-BR: Retorna a posição (índice 0) da primeira ocorrência de sub, ou -1 se não encontrada.
EN-US: Returns the position (0-index) of the first occurrence of sub, or -1 if not found.

let pos = std.string.index_of("hello world", "world")
  // 6
let nao = std.string.index_of("hello", "xyz")
          // -1

split_first(s: string, sep: string) -> string

PT-BR: Retorna a parte antes do primeiro separador.
EN-US: Returns the part before the first separator.

let parte = std.string.split_first("nome:Alice:30", ":")
  // "nome"

split_last(s: string, sep: string) -> string

PT-BR: Retorna a parte após o último separador.
EN-US: Returns the part after the last separator.

let ultima = std.string.split_last("nome:Alice:30", ":")
  // "30"

count_occurrences(s: string, sub: string) -> int

let count = std.string.count_occurrences("banana", "a")
  // 3

is_empty(s: string) -> bool

let vazio = std.string.is_empty("")
        // true
let nao   = std.string.is_empty("hello")
   // false

pad_left(s: string, width: int, pad_char: int) -> string

PT-BR: Preenche a string à esquerda com o caractere especificado até atingir width.
EN-US: Left-pads the string with the specified character until reaching width.

// pad_char é o código Unicode do caractere / pad_char is the Unicode code point
let padded = std.string.pad_left("42", 5, 48)
   // "   42" (48 = '0')
// Nota: 48 é o código de '0', 32 é espaço / Note: 48 is code for '0', 32 is space

pad_right(s: string, width: int, pad_char: int) -> string

let padded = std.string.pad_right("hello", 8, 32)
  // "hello   " (32 = espaço/space)

reverse_str(s: string) -> string

let rev = std.string.reverse_str("hello")
  // "olleh"