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

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

猜你喜欢

转载自www.cnblogs.com/sch01ar/p/9497253.html