Skip to content

Instantly share code, notes, and snippets.

@darkseid4nk
Created December 12, 2024 05:52
Show Gist options
  • Save darkseid4nk/68c632e8422fe3b251a6af3fdb7e1816 to your computer and use it in GitHub Desktop.
Save darkseid4nk/68c632e8422fe3b251a6af3fdb7e1816 to your computer and use it in GitHub Desktop.
Helper functions for strings. Parameter expansions for the win
explode(){
# explode <delimiter> <string> <array_name_to_be_referenced>
# Splits string by delimiter and fills input array where each value is each string that was deparated
# Works on arrays and dicts. Will overwrite if the array is not empty...
[ "$#" -ne "3" ] && return 1
[[ "$(declare -p -- "${3}" 2>/dev/null)" != "declare -a "* ]] && [[ "$(declare -p -- "${3}" 2>/dev/null)" != "declare -A "* ]] && return 1
local -n _arr="$3"
OLDIFS=$IFS
IFS=$1 read -ra _arr <<< "$2"
IFS=$OLDIFS; unset OLDIFS
}
implode(){
# implode <delimiter> "${array_name[@]}"
# Returns a string with array values concatenated together by the delimiter
[[ "$#" -ne "2" ]] && return 1
local separator="$1"
shift
local first="$1"
shift
printf "%s" "$first" "${@/#/$separator}"
return 0
}
lcfirst(){
# lcfirst "string"
# Returns string with the first character of string lowercased
[[ "$#" -ne "1" ]] && return 1
printf "%s\n" "${1,}"
}
lcwords(){
# lcwords "string of words"
# Returns string with the first character of each word being capitalized
[[ "$#" -ne "1" ]] && return 1
set -- $1
local _string=$(echo "${@,}")
printf "%s\n" "$_string"
}
str_empty(){
output_debug "${FUNCNAME}: checking $1"
[[ -z "$1" ]] && return 0 || return 1
}
str_ltrim(){
# str_ltrim " string with leading ws"
# Returns a string with whitespace trimmed from the beginning of the string
shopt -s extglob
printf '%s\n' "${text##+([[:space:]])}"
}
str_notempty(){
output_debug "${FUNCNAME}: checking $1"
[[ ! -z "$1" ]] && return 0 || return 1
}
str_replace(){
# Returns a string with the replaced values on all occurances
# usage: str_replace <search> <replace> <subject>
[[ "$#" -ne 3 ]] && return 1
printf "%s\n" "${3//$1/$2}"
}
str_rtrim(){
# str_rtrim "string with trailing ws "
# Returns a string with whitespace trimmed from the end of the string
shopt -s extglob
printf '%s\n' "${text%%+([[:space:]])}"
}
str_len(){
# Returns the length of a given string
printf "%s\n" "${#1}"
}
str_stripquote(){
# str_stripquote "string"
# Returns string with leading and trailing quotes removed
local _temp="${1%\"}"
local _temp="${_temp#\"}"
printf "%s\n" "${_temp}"
}
str_repeat(){
# str_repeat "characterOrString" <int>
# returns <string> repeated <times> times
[[ "$2" -eq "0" ]] && return 1
printf -- "$1%.0s" $(echo $(seq "$2"))
}
strtolower(){
# strtolower "string"
# Returns a lowercase string
[[ "$#" -ne "1" ]] && return 1
printf "%s\n" "${1,,}"
}
strtoupper(){
# strtoupper "string"
# Returns string with the first character of string being capitalized
[[ "$#" -ne "1" ]] && return 1
printf "%s\n" "${1^^}"
}
ucfirst(){
# ucfirst "string"
# Returns string with the first character of string being capitalized
[[ "$#" -ne "1" ]] && return 1
printf "%s\n" "${1^}"
}
ucwords(){
# ucwords "string of words"
# Returns string with the first character of each word being capitalized
[[ "$#" -ne "1" ]] && return 1
set -- $1
local _string=$(echo "${@^}")
printf "%s\n" "$_string"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment