Transform

str_squeeze

Collapse consecutive repeated characters into a single occurrence. Defaults to squeezing spaces.

Example

str_squeeze "hello   world"      # => hello world
str_squeeze "aabbccdd" "a-c"     # => abcdd

Arguments

  • $1 (string): The string to process
  • $2 (string): Optional: character(s) to squeeze (default: space)

Exit codes

  • 0: Always

Output on stdout

  • String with repeated characters collapsed

str_delete

Delete all occurrences of specified characters from a string.

Example

str_delete "hello world" "lo"   # => he wrd
str_delete "abc123" "0-9"       # => abc

Arguments

  • $1 (string): The string to process
  • $2 (string): Characters (or ranges) to delete, as accepted by tr -d

Exit codes

  • 0: Always

Output on stdout

  • String with specified characters removed

str_reverse

Reverse the characters of a string.

Example

str_reverse "hello"         # => olleh
str_reverse "hello world"   # => dlrow olleh

Arguments

  • ... (string): The string to reverse

Exit codes

  • 0: Always

Output on stdout

  • Reversed string

str_abbreviate

Truncate a string to a maximum length, appending an ellipsis (or custom suffix) when truncation occurs. The total output length (including suffix) will not exceed the specified maximum.

Example

str_abbreviate "hello world" 8         # => hello...
str_abbreviate "hi" 10                 # => hi
str_abbreviate "hello world" 8 "…"    # => hello w…

Arguments

  • $1 (string): The string to abbreviate
  • $2 (int): Maximum total length in characters
  • $3 (string): Optional: suffix to append when truncating (default: '...')

Exit codes

  • 0: Always

Output on stdout

  • Abbreviated string

str_expand_tabs

Expand tab characters to spaces. Requires the expand command.

Example

str_expand_tabs $'col1\tcol2'      # => col1    col2
str_expand_tabs $'a\tb' 4          # => a   b

Arguments

  • $1 (string): The string to process
  • $2 (int): Optional: tab stop width (default: 8)

Exit codes

  • 0: Always

Output on stdout

  • String with tabs replaced by spaces

str_chunk

Split a string into chunks of a given size, printing each chunk on a separate line.

Example

str_chunk "abcdefgh" 3   # => abc
                         #    def
                         #    gh

Arguments

  • $1 (string): The string to chunk
  • $2 (int): Characters per chunk

Exit codes

  • 0: Always

Output on stdout

  • One chunk per line

str_fields

Split a string on a delimiter and return one or more fields. Field numbering is 1-based. If no fields are specified, all fields are printed, one per line.

Example

str_fields "a:b:c" ":"       # => a
                             #    b
                             #    c
str_fields "a:b:c" ":" 2     # => b
str_fields "a:b:c" ":" 1 3   # => a
                             #    c

Arguments

  • $1 (string): The string to split
  • $2 (string): The delimiter character
  • ... (int): Optional: one or more 1-based field numbers to extract

Exit codes

  • 0: Always

Output on stdout

  • One field per line

str_chomp

Remove trailing newlines from a string and print the result.

Arguments

  • ... (string): The input string

Exit codes

  • 0: Always

Output on stdout

  • Input string with trailing newline stripped

chomp

Alias for str_chomp.

str_chop

Remove the last n characters from a string. Defaults to removing 1 character (Perl chop semantics).

Example

str_chop "hello,"        # => hello
str_chop -n 3 "hello..."  # => hello

Arguments

  • $1 (string): Optional: -n followed by count of characters to remove
  • ... (string): The input string

Exit codes

  • 0: Always

Output on stdout

  • Input string with last n characters removed

chop

Alias for str_chop.