美文网首页JuliaJulia语言
Julia快速入门(二)字符串

Julia快速入门(二)字符串

作者: Julia语言 | 来源:发表于2018-11-16 12:55 被阅读20次

    本篇代码在Julia1.0.2中测试通过

    字符串的转换与规格化

    字符串类型转换

    # 字符串可以使用parse来转换成int或者float
    e_str1 = "2.718"
    e = parse(Float64,e_str1)
    
    num_15 = parse(Int, "15")
    println(3num_15)
    # 程序输出: 45
    
    # int和float间的互相转换使用convert
    convert(Float64,123)
    # 输出123.0
    convert(Int,1.0)
    # 输出1
    

    字符串规格化

    Julia1.0.2中有bug,@printf@sprintf暂时用不了

    s = @sprintf "this is a %s %15.1f" "test" 34.567;
    println(s)
    # 输出 UndefVarError: @sprintf not defined
    

    假如没有bug,应该是这样的

    e_str1 = "2.718"
    e = float(e_str1)
    # 使用printf可以将数字转换成字符串并加以规格化
    @printf "e = %0.2f\n" e
    # 程序输出: 2.72
    
    # 或是使用sprintf构建另一个字符串
    e_str2 = @sprintf("%0.3f", e)
    println(e_str2)
    # 程序输出: 2.718
    
    # 同时输出两个字符串:
    println("e_str1 == e_str2: $(e_str1 == e_str2)")
    # 程序输出: e_str1 == e_str2: true
    
    # 可用的数字格式字符有f, e, g, c, s, p, d:
    # (pi是一个预先定义好的常量;但因为它的类型是"MathConst",它必须先要转换为float类型才能被规格化)
    @printf "fix trailing precision: %0.3f\n" float(pi)
    # 程序输出: fix trailing precision: 3.142
    @printf "scientific form: %0.6e\n" 1000pi
    # 程序输出: scientific form: 3.141593e+03
    # g这种用法尚未被指定
    @printf "a character: %c\n" 'α'
    # 程序输出: a character: α
    @printf "a string: %s\n" "look I'm a string!"
    # 程序输出: a string: look I'm a string!
    @printf "right justify a string: %50s\n" "width 50, text right justified!"
    # 程序输出: right justify a string:                    width 50, text right justified!
    @printf "a pointer: %p\n" 100000000
    # 程序输出: a pointer: 0x0000000005f5e100
    @printf "print a integer: %d\n" 1e10
    # 程序输出: print an integer: 10000000000
    

    字符串长度

    length("jμΛIα")
    # 5
    

    字符串重复

    使用repeat或者^

    repeat("ha", 3)
    # "hahaha"
    "Test "^3
    # "Test Test Test "
    

    将数字变成字符串

    pad是占几格,base是几进制

    string(5, base = 13, pad = 4)
    # "0005"
    string(13, base = 5, pad = 4)
    # "0023"
    string("a", 1, true)
    # "a1true"
    

    提取子字符串

    SubString(字符串, 起点, 终点)

    SubString("abc", 1, 2) #SubString(字符串, 起点, 终点)
    # "ab"
    SubString("abc", 1:2) #SubString(字符串, 切片)
    # "ab"
    SubString("abc", 2) #SubString(字符串, 起点)
    # "bc"
    

    字符串比较

    使用==比较

    "abc" == "abc" # true
    "abc" == "αβγ" # false
    

    使用cmp比较
    cmp(a::AbstractString, b::AbstractString) -> Int

    • Return 0 if both strings have the same length and the character at each index is the same in both strings.
    • Return -1 if a is a prefix of b, or if a comes before b in alphabetical order.
    • Return 1 if b is a prefix of a, or if b comes before a in alphabetical order (technically, lexicographical order by Unicode code points).
    • 前面比后面‘大’就是1,相等为0,否则为-1
    cmp("abc", "abc") # 0
    cmp("ab", "abc") # -1
    cmp("abc", "ab") # 1
    cmp("ab", "ac") # -1
    cmp("ac", "ab") # 1
    cmp("α", "a") # 1
    cmp("b", "β") # -1
    

    字符串左右加空格

    lpad("March", 10) # "     March"
    rpad("March", 20) # "March               "
    

    查找

    findfirst

    findfirst("z", "Hello to the world") # returns nothing, but not printed in the REPL
    findfirst("Julia", "JuliaLang") # 1:5
    

    findnext
    findnext(pattern::AbstractString, string::AbstractString, start::Integer)
    findnext(pattern::Regex, string::String, start::Integer)

    findnext("z", "Hello to the world", 1) === nothing # true
    findnext("o", "Hello to the world", 6) # 8:8
    findnext("Lang", "JuliaLang", 2) # 6:9
    

    findlast

    findlast("o", "Hello to the world") # 15:15
    

    findprev
    findprev("substring", string, i):从第i个位置,往前数,找到第一substring,返回其index

    findprev("z", "Hello to the world", 18) === nothing # true
    findprev("o", "Hello to the world", 18) # 15:15
    findprev("Julia", "JuliaLang", 6) # 1:5
    
    欢迎关注微信公众账号Julia语言.jpg

    点击阅读原文可查看历史文章

    相关文章

      网友评论

        本文标题:Julia快速入门(二)字符串

        本文链接:https://www.haomeiwen.com/subject/vheyfqtx.html