Added more string replacements

This commit is contained in:
Dylan Araps
2018-06-15 08:43:35 +10:00
parent 49f91fa6f0
commit ac2b9e7ddf
2 changed files with 54 additions and 6 deletions

View File

@@ -43,8 +43,10 @@ scripts and not full blown utilities.
* [Change a string to lowercase.](#change-a-string-to-lowercase) * [Change a string to lowercase.](#change-a-string-to-lowercase)
* [Change a string to uppercase.](#change-a-string-to-uppercase) * [Change a string to uppercase.](#change-a-string-to-uppercase)
* [Trim quotes from a string.](#trim-quotes-from-a-string) * [Trim quotes from a string.](#trim-quotes-from-a-string)
* [Strip characters from start of string.](#strip-characters-from-start-of-string) * [Strip all instances of pattern from string.](#strip-all-instances-of-pattern-from-string)
* [Strip characters from end of string.](#strip-characters-from-end-of-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) * [Variables](#variables)
* [Assign and access a variable using a variable.](#assign-and-access-a-variable-using-a-variable) * [Assign and access a variable using a variable.](#assign-and-access-a-variable-using-a-variable)
* [Arrays](#arrays) * [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 ```sh
lstrip() { lstrip() {
# Usage: lstrip "string" "chars to remove" # Usage: lstrip "string" "pattern"
printf '%s\n' "${1##$2}" printf '%s\n' "${1##$2}"
} }
``` ```
### Strip characters from end of string. ### Strip pattern from end of string.
```sh ```sh
rstrip() { rstrip() {
# Usage: rstrip "string" "chars to remove" # Usage: rstrip "string" "pattern"
printf '%s\n' "${1%%$2}" printf '%s\n' "${1%%$2}"
} }
``` ```

12
test.sh
View File

@@ -32,6 +32,16 @@ test_trim_quotes() {
assert_equals "$result" "test string" 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() { test_lstrip() {
result="$(lstrip "!:IHello" "!:I")" result="$(lstrip "!:IHello" "!:I")"
assert_equals "$result" "Hello" assert_equals "$result" "Hello"
@@ -139,6 +149,8 @@ main() {
test_lower test_lower
test_upper test_upper
test_trim_quotes test_trim_quotes
test_strip_all
test_strip
test_lstrip test_lstrip
test_rstrip test_rstrip
test_reverse_array test_reverse_array