14.8 Stripping substrings from strings

Function: mbfl_string_strip_carriage_return_var _RV LINE

When exchanging strings with a remote process through a network socket we, usually, need to end a line with a carriage return plus line feed sequence. If we use the command read to read a line from a socket: we can instruct it to discard the ending line feed, yielding us a line ending with a carriage return.

This function strips the ending carriage return from LINE, if any, and stores the result in the result variable _RV. It is fine if LINE is the empty string.

Function: mbfl_string_strip_prefix_var _RV PREFIX STRING

If STRING begins with PREFIX: strip PREFIX from STRING; store the result in the result variable _RV; return true.

If STRING does not begin with PREFIX: store STRING itself in the result variable _RV; return false.

declare RV

mbfl_string_strip_prefix_var RV 'ciao ' 'ciao mamma'  ⇒ 0
"$RV"                                                 ⇒ 'mamma'

mbfl_string_strip_prefix_var RV ''      'ciao mamma'  ⇒ 0
"$RV"                                                 ⇒ 'ciao mamma'

mbfl_string_strip_prefix_var RV ''      ''            ⇒ 0
"$RV"                                                 ⇒ ''

mbfl_string_strip_prefix_var RV 'hello' 'ciao mamma'  ⇒ 1
"$RV"                                                 ⇒ 'ciao mamma'

mbfl_string_strip_prefix_var RV 'hello' ''            ⇒ 1
"$RV"                                                 ⇒ ''
Function: mbfl_string_strip_suffix_var _RV STRING SUFFIX

If STRING ends with SUFFIX: strip SUFFIX from STRING; store the result in the result variable _RV; return true.

If STRING does not end with SUFFIX: store STRING itself in the result variable _RV; return false.

declare RV

mbfl_string_strip_suffix_var RV 'ciao mamma' 'mamma'    ⇒ 0
"$RV"                                                   ⇒ 'ciao '

mbfl_string_strip_suffix_var RV 'ciao mamma' ''         ⇒ 0
"$RV"                                                   ⇒ 'ciao mamma'

mbfl_string_strip_suffix_var RV ''           ''         ⇒ 0
"$RV"                                                   ⇒ ''

mbfl_string_strip_suffix_var RV 'ciao mamma' 'mom'      ⇒ 1
"$RV"                                                   ⇒ 'ciao mamma'

mbfl_string_strip_suffix_var RV ''           'mom'      ⇒ 1
"$RV"                                                   ⇒ ''
Function: mbfl_string_strip_prefix_and_suffix_var _RV PREFIX STRING SUFFIX

If STRING begins with PREFIX and ends with SUFFIX: strip PREFIX and SUFFIX from STRING; store the result in the result variable _RV; return 0.

If STRING begins with PREFIX and does not end with SUFFIX: strip PREFIX from STRING; store the result in the result variable _RV; return 2.

If STRING does not begin with PREFIX and it does end with SUFFIX: strip SUFFIX from STRING; store the result in the result variable _RV; return 3.

Otherwise store STRING itself in the result variable _RV and return 1.

declare RV

mbfl_string_strip_prefix_and_suffix_var RV 'ci'    'ciao mamma'    'ma'    ⇒ 0
"$RV"                                                                      ⇒ 'ao mam'

mbfl_string_strip_prefix_and_suffix_var RV 'ciao'  'ciao mamma' 'mamma'    ⇒ 0
"$RV"                                                                      ⇒ ' '

mbfl_string_strip_prefix_and_suffix_var RV 'ciao ' 'ciao mamma' 'mamma'    ⇒ 0
"$RV"                                                                      ⇒ ''

mbfl_string_strip_prefix_and_suffix_var RV ''      'ciao mamma' ''         ⇒ 0
"$RV"                                                                      ⇒ 'ciao mamma'

mbfl_string_strip_prefix_and_suffix_var RV ''      'ciao mamma' 'mamma'    ⇒ 0
"$RV"                                                                      ⇒ 'ciao '

mbfl_string_strip_prefix_and_suffix_var RV 'ciao'  'ciao mamma' ''         ⇒ 0
"$RV"                                                                      ⇒ ' mamma'

##

mbfl_string_strip_prefix_and_suffix_var RV 'ciao'  'ciao mamma' 'mom'      ⇒ 2
"$RV"                                                                      ⇒ ' mamma'

##

mbfl_string_strip_prefix_and_suffix_var RV 'hello' 'ciao mamma' 'mamma'    ⇒ 3
"$RV"                                                                      ⇒ 'ciao '

##

mbfl_string_strip_prefix_and_suffix_var RV 'hello' 'ciao mamma' 'mom'      ⇒ 1
"$RV"                                                                      ⇒ 'ciao mamma'

This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.