SPECTRALANG_

// DOCS NAVIGATION

std.char — Operações em Caracteres_

SOURCE: content/docs/reference/05-stdlib.md05 Stdlib / 12. std.char — Operações em Caracteres / Character Operations

PT-BR:
As funções de std.char operam sobre códigos Unicode (inteiros), o mesmo formato retornado por std.string.char_at().

EN-US:
Functions in std.char operate on Unicode code points (integers), the same format returned by std.string.char_at().

import std.char

is_alpha(c: int) -> bool

let sim = std.char.is_alpha(65)
     // true ('A')
let nao = std.char.is_alpha(48)
     // false ('0')

is_digit_char(c: int) -> bool

let sim = std.char.is_digit_char(48)
   // true ('0')
let nao = std.char.is_digit_char(65)
   // false ('A')

is_whitespace_char(c: int) -> bool

let sim = std.char.is_whitespace_char(32)
   // true (espaço / space)
let sim2 = std.char.is_whitespace_char(9)
   // true (tab)

is_upper_char(c: int) -> bool / is_lower_char(c: int) -> bool

let upper = std.char.is_upper_char(65)
   // true ('A')
let lower = std.char.is_lower_char(97)
   // true ('a')

is_alphanumeric(c: int) -> bool

let sim = std.char.is_alphanumeric(97)
   // true ('a')
let sim2 = std.char.is_alphanumeric(48)
  // true ('0')
let nao = std.char.is_alphanumeric(32)
   // false (espaço)

to_upper_char(c: int) -> int / to_lower_char(c: int) -> int

let A = std.char.to_upper_char(97)
    // 65 ('A')
let a = std.char.to_lower_char(65)
    // 97 ('a')

Exemplo: Processamento de String Caractere a Caractere

module analisar_string

import std.string as s
import std.char as c
from std.io import println
import std.convert

func contar_digitos(texto: string) returns int {
    let count = 0
    let i = 0
    let len = s.len(texto)
    while i < len {
        let codigo = s.char_at(texto, i)
        if c.is_digit_char(codigo) {
            count = count + 1
        }
        i = i + 1
    }
    return count
}

public func main() {
    let texto = "abc123def456"
    let n = contar_digitos(texto)
    println(f"Dígitos em '{texto}': {n}")
    // 6
}