Skip to content

Instantly share code, notes, and snippets.

@mattmc3 mattmc3/fish_string.md
Last active Oct 20, 2019

Embed
What would you like to do?
ZSH - string functions

fish string utility

string - manipulate strings

Synopsis
   string escape [(-n | --no-quoted)] [--style=xxx] [STRING...]
   string join [(-q | --quiet)] SEP [STRING...]
   string join0 [(-q | --quiet)] [STRING...]
   string length [(-q | --quiet)] [STRING...]
   string lower [(-q | --quiet)] [STRING...]
   string match [(-a | --all)] [(-e | --entire)] [(-i | --ignore-case)] [(-r | --regex)]
                [(-n | --index)] [(-q | --quiet)] [(-v | --invert)] PATTERN [STRING...]
   string repeat [(-n | --count) COUNT] [(-m | --max) MAX] [(-N | --no-newline)]
                 [(-q | --quiet)] [STRING...]
   string replace [(-a | --all)] [(-f | --filter)] [(-i | --ignore-case)] [(-r | --regex)]
                  [(-q | --quiet)] PATTERN REPLACEMENT [STRING...]
   string split [(-m | --max) MAX] [(-n | --no-empty)] [(-q | --quiet)] [(-r | --right)] SEP
                [STRING...]
   string split0 [(-m | --max) MAX] [(-n | --no-empty)] [(-q | --quiet)] [(-r | --right)]
                 [STRING...]
   string sub [(-s | --start) START] [(-l | --length) LENGTH] [(-q | --quiet)]
              [STRING...]
   string trim [(-l | --left)] [(-r | --right)] [(-c | --chars CHARS)]
               [(-q | --quiet)] [STRING...]
   string unescape [--style=xxx] [STRING...]
   string upper [(-q | --quiet)] [STRING...]
# fish has a utility for string functions called `string` utility.
# This is how you can do the same things with vanilla ZSH
mystr="hOw Do I dO cOoL sTrInG sTuFf In ZsH?"
### string escape
# TODO
### string join
function join_by { local IFS="$1"; shift; echo "$*"; }
join_by , abc "1 2 3" lmnop "q r s"
### string join0
# TODO
### string length
echo ${#mystr}
### string lower
# using :l in zsh
echo "${mystr:l}"
# using tr
echo $mystr | tr "[:upper:]" "[:lower:]"
### string match
# TODO
### string repeat
# using printf
printf "$mystr%.0s" {1..3}
### string replace
# TODO
### string split
# using awk
echo "a|b|c" | awk -v RS='|' '1'
# using tr
echo "a|b|c" | tr "|" "\n"
### string split0
# using tr
echo -e "abc\x00def\x00ghi" | tr "\0" "\n"
### string sub
# TODO
### string trim
# using awk
strtrim="\t\t \t \t text \t\t\t \t"
echo $strtrim | awk '{$1=$1};1'
### string unescape
# TODO
### string upper
# using :u in zsh
echo "${mystr:u}"
# using tr
echo $mystr | tr "[:lower:]" "[:upper:]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.