String manipulation

Hello everyone,
I was wondering if there is set of builtin functions to operate on strings in nix.
Like converting string to uppercase or lowercase,use sub-sequences.
Thanks

Yes, there’s lib.toUpper and lib.toLower. Play around with them using nix repl

nix-repl> builtins.substring 0 4 "hello, world" 
"hell"
nix-repl> lib.toUpper "hello, world"
"HELLO, WORLD"
nix-repl> lib.toLower "HELLO, WORLD"  
"hello, world"

Thx for the reply but here is the full output after running that in nix-repl

nix-repl> lib.toUpper “nix repl”
error: undefined variable ‘lib’

   at «string»:1:1:

        1| lib.toUpper "nix repl"
         | ^

nix-repl>

you need to import lib. if you’re using flakes nix repl nixpkgs will add it. Otherwise :l <nixpkgs> Apologies, I should have made that clear

1 Like