s1 = "The quick brown fox jumps over the lazy dog α,β,γ"
# returns the first index of a char
i = search(s1, 'b')
println(i)
#> 11
# the second argument is equivalent to the second argument of split, see below
# or a range if called with another string
r = search(s1, "brown")
println(typeof(r))
#> UnitRange{Int64}
println(r)
#> 11:15
# string replace is done thus:
r = replace(s1, "brown", "red")
show(r); println()
#> "The quick red fox jumps over the lazy dog"
# search and replace can also take a [regular expressions] by preceding the string with 'r'
r = search(s1, r"b[\w]*n") # 这里[\w]为固定格式
println(r)
#> 11:15
# again with a regular expression
r = replace(s1, r"b[\w]*n", "red")
show(r); println()
#> "The quick red fox jumps over the lazy dog"
# there are also functions for regular expressions that return RegexMatch types
# [match] scans left to right for the first match (specified starting index optional)
r = match(r"b[\w]*n", s1)
println(r)
#> RegexMatch("brown")
# RegexMatch types have a property match that holds the matched string
show(r.match); println()
#> "brown"
# [matchall] returns a vector with RegexMatches for each match
r = matchall(r"[\w]{5,}", s1) # 寻找s1中字符长度大于等于5的
println(r)
#> SubString{String}["quick", "brown", "jumps"]
# [eachmatch] returns an iterator over all the matches
r = eachmatch(r"[\w]{5,}", s1)
for i in r
print("\"$(i.match)\" ")
end
println()
#> "quick" "brown" "jumps"
# a string can be repeated using the [repeat] function,
# or more succinctly with the [^ syntax]:
r = "hello "^3
show(r); println() #> "hello hello hello "
# the [strip] function works the same as python:
# e.g., with one argument it strips the outer whitespace # 去掉外部空格
r = strip(" hello ")
show(r); println() #> "hello"
# or with a second argument of an array of chars it strips any of them;
r = strip("hello ", ['h', ' ']) # or r = strip("hello ", 'h')
show(r); println() #> "ello"
# (note the array is of chars and not strings)
# similarly [split] works in basically the same way as python:
r = split("hello, there,bob", ',') # 一个字符用单引号
show(r); println() #> ["hello"," there","bob"]
r = split("hello, there,bob", ", ") # 多于1个字符用双引号
show(r); println() #> ["hello","there,bob"]
r = split("hello, there,bob", [',', ' '], limit=0, keep=false)
show(r); println() #> ["hello","there","bob"]
# (the last two arguements are limit and include_empty, see docs)
# the opposite of split: [join] is simply
# join函数加添相应的符号
r = join(collect(1:10), ", ")
println(r) #> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
julia:字符串操作
猜你喜欢
转载自blog.csdn.net/chd_lkl/article/details/82824241
今日推荐
周排行