-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.sh
96 lines (78 loc) · 1.71 KB
/
string.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
##
# Changes the string to upper case.
STRING_upcase() {
tr '[:lower:]' '[:upper:]' <<< $*
}
##
# Changes the string to lower case.
STRING_downcase() {
tr '[:upper:]' '[:lower:]' <<< $*
}
##
# Changes the case of the string to upper or lower.
STRING_change_case() {
local direction="$( STRING_downcase "$1" )"
local string="$2"
case $direction in
up|u|upper|big)
STRING_upcase "$string"
;;
down|d|l|lower|small)
STRING_downcase "$string"
;;
esac
}
##
# Removes leading and trailing space from string.
STRING_strip() {
echo "$*" | xargs
}
##
# Removes ALL spaces from string.
STRING_trim_all_spaces() {
tr -d '[:space:]' <<< $*
}
##
# Returns string with all consective spaces replaced by a single space.
STRING_uniform_spacing() {
echo $* | xargs
}
##
# True if string begins with substr.
STRING_start_with() {
local substr="$1"
local string="$2"
[[ $string == $substr* ]] && return 0 || return 1
}
##
# True if string ends with substr.
STRING_end_with() {
local substr="$1"
local string="$2"
[[ $string == *$substr ]] && return 0 || return 1
}
##
# True if string contains substr.
STRING_include() {
local substr="$1"
local string="$2"
[[ $string == *$substr* ]] && return 0 || return 1
}
##
# Returns length of string.
STRING_length() {
local string="$1"
echo "${#string}"
}
##
# If string contains no characters, including spaces.
STRING_empty() {
local string="$1"
(( ${#string} == 0 )) && return 0 || return 1
}
##
# If string is nothing but whitespace or empty.
STRING_blank() {
local string="$( STRING_strip "$1" )"
(( ${#string} == 0 )) && return 0 || return 1
}