@@ -137,13 +137,15 @@ end
137
137
const _default_delims = [' ' ,' \t ' ,' \n ' ,' \v ' ,' \f ' ,' \r ' ]
138
138
139
139
"""
140
- lstrip(s ::AbstractString[, chars::Chars ])
140
+ lstrip(str ::AbstractString[, chars])
141
141
142
- Return `s` with any leading whitespace and delimiters removed.
143
- The default delimiters to remove are `' '`, `\\ t`, `\\ n`, `\\ v`,
144
- `\\ f`, and `\\ r`.
145
- If `chars` (a character, or vector or set of characters) is provided,
146
- instead remove characters contained in it.
142
+ Remove leading characters from `str`.
143
+
144
+ The default behaviour is to remove leading whitespace and delimiters: see
145
+ [`isspace`](@ref) for precise details.
146
+
147
+ The optional `chars` argument specifies which characters to remove: it can be a single character,
148
+ vector or set of characters, or a predicate function.
147
149
148
150
# Examples
149
151
```jldoctest
@@ -154,22 +156,25 @@ julia> lstrip(a)
154
156
"March"
155
157
```
156
158
"""
157
- function lstrip (s:: AbstractString , chars :: Chars = _default_delims )
159
+ function lstrip (s:: AbstractString , f = isspace )
158
160
e = lastindex (s)
159
161
for (i, c) in pairs (s)
160
- ! (c in chars ) && return SubString (s, i, e)
162
+ ! f (c ) && return SubString (s, i, e)
161
163
end
162
164
SubString (s, e+ 1 , e)
163
165
end
166
+ lstrip (s:: AbstractString , chars:: Chars ) = lstrip (s, in (chars))
164
167
165
168
"""
166
- rstrip(s::AbstractString[, chars::Chars])
169
+ rstrip(str::AbstractString[, chars])
170
+
171
+ Remove trailing characters from `str`.
167
172
168
- Return `s` with any trailing whitespace and delimiters removed.
169
- The default delimiters to remove are `' '`, ` \\ t`, ` \\ n`, ` \\ v`,
170
- ` \\ f`, and ` \\ r`.
171
- If `chars` (a character, or vector or set of characters) is provided ,
172
- instead remove characters contained in it .
173
+ The default behaviour is to remove leading whitespace and delimiters: see
174
+ [`isspace`](@ref) for precise details.
175
+
176
+ The optional `chars` argument specifies which characters to remove: it can be a single character ,
177
+ vector or set of characters, or a predicate function .
173
178
174
179
# Examples
175
180
```jldoctest
@@ -180,19 +185,24 @@ julia> rstrip(a)
180
185
"March"
181
186
```
182
187
"""
183
- function rstrip (s:: AbstractString , chars :: Chars = _default_delims )
188
+ function rstrip (s:: AbstractString , f = isspace )
184
189
for (i, c) in Iterators. reverse (pairs (s))
185
- c in chars || return SubString (s, 1 , i)
190
+ f (c) in chars || return SubString (s, 1 , i)
186
191
end
187
192
SubString (s, 1 , 0 )
188
193
end
194
+ rstrip (s, chars:: Chars ) = rstrip (s, in (chars))
189
195
190
196
"""
191
- strip(s::AbstractString, [chars::Chars])
197
+ strip(str::AbstractString, [chars])
198
+
199
+ Remove leading and trailing characters from `str`.
200
+
201
+ The default behaviour is to remove leading whitespace and delimiters: see
202
+ [`isspace`](@ref) for precise details.
192
203
193
- Return `s` with any leading and trailing whitespace removed.
194
- If `chars` (a character, or vector or set of characters) is provided,
195
- instead remove characters contained in it.
204
+ The optional `chars` argument specifies which characters to remove: it can be a single character,
205
+ vector or set of characters, or a predicate function.
196
206
197
207
# Examples
198
208
```jldoctest
@@ -201,7 +211,7 @@ julia> strip("{3, 5}\\n", ['{', '}', '\\n'])
201
211
```
202
212
"""
203
213
strip (s:: AbstractString ) = lstrip (rstrip (s))
204
- strip (s:: AbstractString , chars:: Chars ) = lstrip (rstrip (s, chars), chars)
214
+ strip (s:: AbstractString , chars) = lstrip (rstrip (s, chars), chars)
205
215
206
216
# # string padding functions ##
207
217
0 commit comments