-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
first and last with nchar #23960
first and last with nchar #23960
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -610,3 +610,50 @@ function filter(f, s::AbstractString) | |
end | ||
String(take!(out)) | ||
end | ||
|
||
## string first and last ## | ||
|
||
""" | ||
first(str::AbstractString, nchar::Integer) | ||
|
||
Get a string consisting of first `nchar` characters of `str`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "the first"? Same for "last". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
```jldoctest | ||
julia> first("∀ϵ≠0: ϵ²>0", 0) | ||
"" | ||
|
||
julia> first("∀ϵ≠0: ϵ²>0", 1) | ||
"∀" | ||
|
||
julia> first("∀ϵ≠0: ϵ²>0", 3) | ||
"∀ϵ≠" | ||
``` | ||
""" | ||
function first(str::AbstractString, nchar::Integer) | ||
nchar == 0 && return "" | ||
nchar == 1 && return str[1:1] | ||
str[1:nextind(str, 1, nchar-1)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it will give a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was intended. The idea is to have an invariant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we changed it to "at most" nchar elements of the string, then it would be similar to My feeling is that using "at most" nchar elements would be a big more flexible, and it wouldn't hurt the uses where you want exactly nchar elements except that it wouldn't throw an error for a string of the wrong size. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. I will make a PR to have a clear decision point. |
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it return a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My recommendation in #23765 is that not. Usually |
||
|
||
""" | ||
last(str::AbstractString, nchar::Integer) | ||
|
||
Get a string consisting of last `nchar` characters of `str`. | ||
|
||
```jldoctest | ||
julia> last("∀ϵ≠0: ϵ²>0", 0) | ||
"" | ||
|
||
julia> last("∀ϵ≠0: ϵ²>0", 1) | ||
"0" | ||
|
||
julia> last("∀ϵ≠0: ϵ²>0", 3) | ||
"²>0" | ||
``` | ||
""" | ||
function last(str::AbstractString, nchar::Integer) | ||
nchar == 0 && return "" | ||
e = endof(str) | ||
nchar == 1 && return str[e:e] | ||
str[prevind(str, e, nchar-1):e] | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"substring" could be confused with
SubString
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced "substring" with "string"