0%

string

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
set -e

# isEmpty

is_empty(){
local string=$1
[ -n $1 ]
}

# length
# 获取字符串长度
# 例如 length "123456789"
length(){
local string=$1
# expr length $string
echo "${#string}"
}

# 字符串是否相等
equals(){
local string=$1
local compare_with=$2
[[ $string == $compare_with ]]
}

# 字符串是否包含匹配表达式,
contains(){
local string=$1
local pattern=$2
[[ $string =~ $pattern ]]
}

# 字符串截取
# 例如
# substr "123456789"
# substr "123456789" 2
# substr "123456789" 2 -3
# substr "123456789" 2 4
substr(){
local string=$1
# 截取的起始位置
# 默认从 0 开始
local position=${2:-0}
# 截取的长度
# 默认是最大长度
# 为负数时, 即自后向前截取, `$length + $position` 不能超过字符串的总长度
# 为正数时, 长度可以超过字符串的总长度
# $position 与 $length 为闭开区间
local length=${3:-${#string}}
# expr substr $str $position $length
echo ${string:position:length}
}

# 字符串删除从开头开始最短匹配的表达式
# 例如 cut_from_begin_by_min_matches "abcdefgabcdefg" "a*d"
cut_from_begin_by_min_matches(){
local string=$1
# 需要匹配的子字符串
local pattern=$2
echo ${string#$pattern}
}

# 字符串删除从开头开始最长匹配的表达式
# 例如 cut_from_begin_by_max_matches "abcdefgabcdefg" "a*d"
cut_from_begin_by_max_matches(){
local string=$1
# 需要匹配的子字符串
local pattern=$2
echo ${string##$pattern}
}

# 字符串删除从结尾开始最短匹配的表达式
# 例如 cut_from_end_by_min_matches "abcdefgabcdefg" "d*g"
cut_from_end_by_min_matches(){
local string=$1
# 需要匹配的子字符串
local pattern=$2
echo ${string%$pattern}
}

# 字符串删除从结尾开始最长匹配的表达式
# 例如 cut_from_end_by_max_matches "abcdefgabcdefg" "d*g"
cut_from_end_by_max_matches(){
local string=$1
# 需要匹配的子字符串
local pattern=$2
echo ${string%%$pattern}
}

# 最长匹配表达式, 替换第一个符合条件的子字符串
# 例如
# replace_first "abcdefgabcdefg" "b*d"
# replace_first "abcdefgabcdefg" "b*d" "zzz"
replace_first(){
local string=$1
# 需要匹配的子字符串
local pattern=$2
# 需要替换成的字符串
local replace_to=$3
echo ${string/$pattern/$replace_to}
}

# 最长匹配表达式, 替换第一个符合条件的子字符串
# 例如 replace_from_begin "abcdefgabcdefg" "a*d"
replace_from_begin(){
local string=$1
# 需要匹配的子字符串
local pattern=$2
# 需要替换成的字符串
local replace_to=$3
echo ${string/#$pattern/$replace_to}
}

# 最长匹配表达式, 替换第一个符合条件的子字符串
# 例如 replace_from_end "abcdefgabcdefg" "c*g"
replace_from_end(){
local string=$1
# 需要匹配的子字符串
local pattern=$2
# 需要替换成的字符串
local replace_to=$3
echo ${string/%$pattern/$replace_to}
}

# 最长匹配表达式, 替换所有符合条件的子字符串
# 例如 replace_all "abcdefgabcdefg" "a"
replace_all(){
local string=$1
# 需要匹配的子字符串
local pattern=$2
# 需要替换成的字符串
local replace_to=$3
echo ${string//$pattern/$replace_to}
}

# 字符所在位置
indexOf(){
local string=$1
# $substring 只能是单个字符, 如果是包含多个字符的字符串, 也只匹配第一个字符
local substring=$2
expr index $string $substring
}

is_number(){
[[ $param != +([0-9]) ]]
}

to_number(){
local string=$1
echo $1 | awk '{print int($0)}'
}