From ac2b9e7ddf793b4c1de94e8296f6c801fcd925f5 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Fri, 15 Jun 2018 08:43:35 +1000 Subject: [PATCH] Added more string replacements --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++------ test.sh | 12 ++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a0fd281..a268898 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,10 @@ scripts and not full blown utilities. * [Change a string to lowercase.](#change-a-string-to-lowercase) * [Change a string to uppercase.](#change-a-string-to-uppercase) * [Trim quotes from a string.](#trim-quotes-from-a-string) - * [Strip characters from start of string.](#strip-characters-from-start-of-string) - * [Strip characters from end of string.](#strip-characters-from-end-of-string) + * [Strip all instances of pattern from string.](#strip-all-instances-of-pattern-from-string) + * [Strip first occurrence of pattern from string.](#strip-first-occurrence-of-pattern-from-string) + * [Strip pattern from start of string.](#strip-pattern-from-start-of-string) + * [Strip pattern from end of string.](#strip-pattern-from-end-of-string) * [Variables](#variables) * [Assign and access a variable using a variable.](#assign-and-access-a-variable-using-a-variable) * [Arrays](#arrays) @@ -213,20 +215,54 @@ trim_quotes() { } ``` -### Strip characters from start of string. +### Strip all instances of pattern from string. + +```sh +strip_all() { + # Usage: strip_all "string" "pattern" + printf '%s\n' "${1//$2}" +} + +# Examples: + +# Output: "Th Qck Brwn Fx" +: strip_all "The Quick Brown Fox" "[aeiou]" + +# Output: "TheQuickBrownFox" +: strip_all "The Quick Brown Fox" "[[:space:]]" +``` + +### Strip first occurrence of pattern from string. + +```sh +strip() { + # Usage: strip "string" "pattern" + printf '%s\n' "${1/$2}" +} + +# Examples: + +# Output: "Th Quick Brown Fox" +: strip_all "The Quick Brown Fox" "[aeiou]" + +# Output: "TheQuick Brown Fox" +: strip_all "The Quick Brown Fox" "[[:space:]]" +``` + +### Strip pattern from start of string. ```sh lstrip() { - # Usage: lstrip "string" "chars to remove" + # Usage: lstrip "string" "pattern" printf '%s\n' "${1##$2}" } ``` -### Strip characters from end of string. +### Strip pattern from end of string. ```sh rstrip() { - # Usage: rstrip "string" "chars to remove" + # Usage: rstrip "string" "pattern" printf '%s\n' "${1%%$2}" } ``` diff --git a/test.sh b/test.sh index 4a92ad7..5d114b0 100755 --- a/test.sh +++ b/test.sh @@ -32,6 +32,16 @@ test_trim_quotes() { assert_equals "$result" "test string" } +test_strip_all() { + result="$(strip_all "The Quick Brown Fox" "[aeiou]")" + assert_equals "$result" "Th Qck Brwn Fx" +} + +test_strip() { + result="$(strip "The Quick Brown Fox" "[aeiou]")" + assert_equals "$result" "Th Quick Brown Fox" +} + test_lstrip() { result="$(lstrip "!:IHello" "!:I")" assert_equals "$result" "Hello" @@ -139,6 +149,8 @@ main() { test_lower test_upper test_trim_quotes + test_strip_all + test_strip test_lstrip test_rstrip test_reverse_array