Questions about string

zhangchunyong
Wed, 12 Oct 2022 03:51:49 +0000
New to Julia

I want the number before the string c= “9S8M13S” 's S and then return 9 and 13.And I tried following.

julia> c="9S8M13S"
"9S8M13S"
julia> fs=findall(r"\d*S",c)
2-element Vector{UnitRange{Int64}}:
 1:2
 5:7
julia> first.(fs)
2-element Vector{Int64}:
 1
 5
julia> last.(fs).-1
2-element Vector{Int64}:
 1
 6
julia> z=zip(first.(fs),last.(fs).-1)
zip([1, 5], [1, 6])

a=Int[]

for (i,j) in z
    push!(a,parse(Int,c[i:j]))
end

julia> a
2-element Vector{Int64}:
  9
 13

Is there a more direct way to do it? I think it is a little bit undirect using this method.

22 posts - 7 participants