zoukankan      html  css  js  c++  java
  • Julia

    字符

    字符使用单引号括起来,字符是 32 位整数

    julia> 'a'
    'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
    
    julia> typeof(ans)
    Char
    
    julia> Int('a')
    97
    
    julia> typeof(ans)
    Int64
    

    字符对应的整数是相对应的 ASCII 码值

    也可以把整数转换为相对应的字符

    julia> Char(97)
    'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
    

     对字符进行比较

    julia> 'a' > 'C'
    true
    

    对字符进行算术运算

    julia> 'a' - 'b'
    -1
    
    julia> 'a' + 1
    'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
    

    比较运算和算术运算都是根据 ASCII 码的值进行的

    字符串

    字符串使用双引号或三个双引号括起来

    julia> a = "Hello World"
    "Hello World"
    
    julia> a = """Hello World"""
    "Hello World"
    

    字符串索引

    julia> a = "Hello World"
    "Hello World"
    
    julia> a[1]
    'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)
    
    julia> a[end]
    'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
    
    julia> a[end - 1]
    'l': ASCII/Unicode U+006c (category Ll: Letter, lowercase)
    

    第一个索引是 1,而不是 0

    关键字 end 为最后的索引,可以对 end 进行算术运算,end - 1 为倒二个索引值

    索引不能小于 1,不能大于 end,索引不能为 0 和负数

    使用范围索引提取字符串

    julia> a = "Hello World"
    "Hello World"
    
    julia> a[2:4]  # 索引 2 到 4 的字符串
    "ell"
    

    字符串的比较

    julia> "hello" < "world"
    true
    

    字符串的比较是一个个字符逐个比较

    字符串处理函数

    使用 search() 函数查找某个字符的索引值

    julia> search("Hello World", 'r')
    9
    
    julia> search("Hello World", 'q')  # 如果字符串中没有该字符,结果为 0
    0
    
    julia> search("Hello World", 'r', 3)  # 从索引 3 开始查找
    9
    
    julia> search("Hello World", 'e', 3)
    0
    

    repeat() 函数进行复制字符串

    julia> repeat("abc", 3)  # 将字符串 abc 复制 3 次
    "abcabcabc"
    

    length() 获取字符串的长度

    julia> length("hello")
    5
    

    firstindex(str) 函数获取字符串 str 第一个索引,对于字符串总是 1,其它容器就不一样了

    julia> firstindex("hello")
    1
    

    lastindex(str) 函数获取字符串 str 最大的索引,就是 end

    julia> lastindex("hello")
    5
    

    ncodeunits(str) 函数获取字符串 str 中的代码单元数

    julia> ncodeunits("hello world")
    11
    

    codeunit(str, i) 函数获取索引为 i 的字符串 str 中的代码单元值

    julia> codeunit("hello world", 2)
    0x65
    

    nextind(str, i, n=1) 函数获取字符串 str 索引为 i 之后第 n 个字符的索引

    prevind(str, i, n=1) 函数获取字符串 str 索引为 i 之前第 n 个字符的索引

    julia> nextind("hello world", 3, 2)
    5
    
    julia> prevind("hello world", 3, 2)
    1
    

    thisind(str, i) 函数获取字符串 str 中的索引为 i 的字符的第一个索引

    如果 i 等于 0 或 ncodeunits(s)+1 返回 i,在所有其他情况下抛出 BoundsError

    julia> thisind("a", 0)
    0
    
    julia> thisind("a", 1)
    1
    
    julia> thisind("a", 2)
    2
    
    julia> thisind("a", 3)
    ERROR: BoundsError: attempt to access "a"
      at index [3]
    Stacktrace:
     [1] _thisind_str(::String, ::Int64) at .stringsstring.jl:117
     [2] thisind(::String, ::Int64) at .stringsstring.jl:110
     [3] top-level scope at none:0
    

    join() 函数将一个字符串数组连接成一个字符串,在相邻字符串之间插入给定的分隔符

    julia> join(["hello", "world", "julia"], "+", "-")
    "hello+world-julia"
    
    julia> join(["hello", "world", "julia"], "+")
    "hello+world+julia"
    

    findfirst() 函数可以搜索特定字符的索引

    julia> findfirst("w", "hello world")
    7:7
    
    julia> findfirst("o", "hello world")
    5:5
    
    julia> findfirst("a", "hello world")
    
    julia> findfirst("lo", "hello world")
    4:5
    
    julia> findfirst("le", "hello world")
    

    搜索的字符为该字符第一次出现的索引

    如果字符串中没有该字符则返回空

    如果搜索一串字符串,则返回索引范围

    如果字符串有一个字符不在原字符串中,返回空

    findnext() 函数可以带有第三个参数,并从第三个参数开始搜索给定偏移处的字符

    julia> findnext("a", "hello world", 1)  # 如果字符串不在原字符串中返回空
    
    julia> findnext("o", "hello world", 1)  # 从索引 1 开始查找第一个 o 的索引
    5:5
    
    julia> findnext("o", "hello world", 6)  # 从索引 6 开始查找第一个 o 的索引
    8:8
    

    findprev(str1, str2, i) 从字符串 str2 中索引为 i 的位置开始向前匹配字符串 str2

    返回值也是找到匹配序列的索引范围

    julia> findprev("a", "hello world", 11)
    
    julia> findprev("o", "hello world", 11)
    8:8
    
    julia> findprev("lo", "hello world", 11)
    4:5
    

    occursin(str1, str2) 函数查看字符串 str1 是否在字符串 str2 中,返回 Bool 值

    julia> occursin("o", "hello world")
    true
    
    julia> occursin("a", "hello world")
    false
    
    julia> occursin("lo", "hello world")
    true
    
    julia> occursin("le", "hello world")
    false
    

    reverse() 函数将字符串进行反转

    julia> reverse("hello-world")
    "dlrow-olleh"
    

    replace(str, str1 => str2, [count = Integer]) 函数将字符串 str 中的字符串 str1 替换成字符串 str2,count 为替换的次数,是可选项

    julia> replace("hello python", "python" => "julia")
    "hello julia"
    
    julia> replace("hello python, hello python game", "python" => "julia", count = 1)
    "hello julia, hello python game"
    
    julia> replace("hello pytho", "python" => "julia")
    "hello pytho"
    

    split(str, str1, [limite = Integer], [keepempty=Bool]) 函数将字符串 str 按照 str1 进行分割成数组

    limit 为最多分割成的数组个数

    keepempty 为是否在结果中保留空字段,默认为 true

    julia> split("www.baidu.com", ".")
    3-element Array{SubString{String},1}:
     "www"
     "baidu"
     "com"
    
    julia> split("www.baidu.com", ".", limit = 1)
    1-element Array{SubString{String},1}:
     "www.baidu.com"
    
    julia> split("www.baidu.com", ".", limit = 2)
    2-element Array{SubString{String},1}:
     "www"
     "baidu.com"
    
    julia> split("www.baidu.com", ".", limit = 3)
    3-element Array{SubString{String},1}:
     "www"
     "baidu"
     "com"
    
    julia> split("www.baidu.com", ".", limit = 4)
    3-element Array{SubString{String},1}:
     "www"
     "baidu"
     "com"
    
    julia> split("www.baidu..com", ".")
    4-element Array{SubString{String},1}:
     "www"
     "baidu"
     ""
     "com"
    
    julia> split("www.baidu..com", ".", keepempty = true)
    4-element Array{SubString{String},1}:
     "www"
     "baidu"
     ""
     "com"
    
    julia> split("www.baidu..com", ".", keepempty = false)
    3-element Array{SubString{String},1}:
     "www"
     "baidu"
     "com"
    

    rsplit(str, str1, [limite = Integer], [keepempty=Bool]) 函数和 split() 函数一样,只不过是反过来进行分割的

    julia> rsplit("www.baidu.com", ".")
    3-element Array{SubString{String},1}:
     "www"
     "baidu"
     "com"
    
    julia> rsplit("www.baidu.com", ".", limit = 1)
    1-element Array{SubString{String},1}:
     "www.baidu.com"
    
    julia> rsplit("www.baidu.com", ".", limit = 2)
    2-element Array{SubString{String},1}:
     "www.baidu"
     "com"
    
    julia> rsplit("www.baidu.com", ".", limit = 3)
    3-element Array{SubString{String},1}:
     "www"
     "baidu"
     "com"
    

    strip(str, [chars]) 函数默认去除字符串两边的空格,如果有 chars,则去除字符串两边的 chars

    julia> strip("  hello world   ")
    "hello world"
    
    julia> strip("-hello world+", '+')
    "-hello world"
    
    julia> strip("-hello world+", ['-', '+'])
    "hello world"
    
    julia> strip("-hello+world+", '+')
    "-hello+world"
    

    lstrip(str, [chars]) 函数去除字符串左边的空格或 chars

    rstrip(str, [chars]) 函数去除字符串右边的空格或 chars

    julia> lstrip("   julia    ")
    "julia    "
    
    julia> rstrip("   julia    ")
    "   julia"
    

    startswith(str1, str2) 判断字符串 str1 是否以 str2 开头,返回 Bool 值

    endswith(str1, str2) 判断字符串 str1 是否以 str2 结尾,返回 Bool 值

    julia> startswith("julia", "ju")
    true
    
    julia> endswith("julia", "ia")
    true
    

    first(str, n) 函数获取字符串 str 前 n 个字符

    julia> first("julia", 0)
    ""
    
    julia> first("julia", 1)
    "j"
    
    julia> first("julia", 2)
    "ju"
    
    julia> first("julia", 3)
    "jul"
    
    julia> first("julia", 4)
    "juli"
    

    last(str, n) 函数获取字符串 str 后 n 个字符

    julia> last("julia", 0)
    ""
    
    julia> last("julia", 1)
    "a"
    
    julia> last("julia", 2)
    "ia"
    
    julia> last("julia", 3)
    "lia"
    
    julia> last("julia", 4)
    "ulia"
    

    uppercase(str) 函数将字符串 str 中的字母全转换成大写

    julia> uppercase("julia")
    "JULIA"
    

     lowercase(str) 函数将字符串 str 中的字母全转换成小写

    julia> lowercase("JuLiA")
    "julia"
    

    titlecase(str) 函数将字符串 str 中的字符串按照标题格式进行大小写转换

    julia> titlecase("this is a julia program")
    "This Is A Julia Program"
    

    uppercasefirst(str) 函数将字符串 str 中开头的字母转换为大写

    lowercasefirst(str) 函数将字符串 str 中开头的字母转换为小写

    julia> uppercasefirst("hello julia")
    "Hello julia"
    
    julia> lowercasefirst("HelLo julia")
    "helLo julia"
    

    chop(str, head=Integer, tail=Integer) 函数删除字符串 str 的前 head 个字符和后 tail 个字符,默认删除最后一个字符

    julia> chop("julia")
    "juli"
    
    julia> chop("julia", head=1)
    "uli"
    
    julia> chop("julia", head=1, tail=3)
    "u"
    
    julia> chop("julia", head=3, tail=3)
    ""
    

    如果要删除的字符数超过字符串的长度,则会返回空

    chomp(str) 函数删除换行符

    julia> "julia
    "
    "julia
    "
    
    julia> chomp("julia
    ")
    "julia"
    

    Symbol(str1, str2, ...) 通过字符串参数连接在一起来创建符号

    julia> Symbol("hello", "julia")
    :hellojulia
    
    julia> Symbol("hello", "julia", "and", "python")
    :hellojuliaandpython
    
    julia> Symbol("Day", 5)
    :Day5
    

    escape_string() 一般转义传统的 C 和 Unicode 转义序列,第一个表单返回转义字符串,第二个表单将结果打印到 io

    反斜杠()使用双反斜杠(\)进行转义,不可以打印的字符使用标准的 C 转义码转义,“ 0”表示 NUL(如果明确),unicode 代码点(“ u”前缀)或 hex(“ x”前缀)

    可选的 esc 参数指定任何其他字符,这些字符也应该通过前缀反斜杠进行转义,在第一个表单中默认也会转义

    julia> escape_string("aaa
    bbb")
    "aaa\nbbb"
    
    julia> escape_string("xfexff")
    "\xfe\xff"
    
    julia> escape_string(string('u2135',''))
    "ℵ\0"
    
    julia> escape_string(string('u2135','','0'))
    "ℵ\x000"
    

    unescape_string() 一般转义传统的 C 和 Unicode 转义序列的,第一个表单返回转义字符串,第二个表单将结果打印到 io

    转义序列:

    • 转义为反斜杠(\)
    • 转义双引号(")
    • 标准 C 转义序列( a, b, t, n, v, f, r, e)
    • Unicode 代码点(u 或 U 前缀,1-4 个尾随十六进制数字)
    • 十六进制字节(x,带有 1-2 个尾随十六进制数字)
    • 八进制字节(具有 1-3 个尾随八进制数字)
    julia> unescape_string("aaa\nbbb")  # C 转义序列
    
    julia> unescape_string("\u03c0")  # unicode
    "π"
    
    julia> unescape_string("\101")  # 八进制
    "A"
    

    内插

    使用 string() 函数进行字符串拼接,字符串连接和内插都调用 string 函数来把对象转换为 String

    julia> a = "Hello"
    "Hello"
    
    julia> b = "World"
    "World"
    
    julia> string(a, b)
    "HelloWorld"
    

    字符串不能用“+”进行拼接

    julia> "aaa"+"bbb"
    ERROR: MethodError: no method matching +(::String, ::String)
    Closest candidates are:
      +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
    Stacktrace:
     [1] top-level scope at none:0
    

    Julia 使用 $ 内插字符串

    julia> a = "Hello"
    "Hello"
    
    julia> b = "World"
    "World"
    
    julia> "$a $b"
    "Hello World"
    

    $ 将其后的最短的完整表达式内插进字符串

    可以使用小括号通过 $ 将任意表达式进行内插

    julia> a = "Hello"
    "Hello"
    
    julia> b = "World"
    "World"
    
    julia> "$a $b, $(1 + 2)"
    "Hello World, 3"
    

    如果需要在字符串中使用 $,要进行转义

    julia> a = "Hello"
    "Hello"
    
    julia> b = "World"
    "World"
    
    julia> "$a + $b"
    "$a + $b"
    
  • 相关阅读:
    电路原理图基本知识概述(转)
    数字电路笔记
    模拟电路笔记
    ROS笔记一
    STM32笔记三
    电子元件笔记
    STM32笔记二
    C语言相关知识
    利用sql报错帮助进行sql注入
    kali下纯文本与窗口环境切换
  • 原文地址:https://www.cnblogs.com/sch01ar/p/9497253.html
Copyright © 2011-2022 走看看