Julia 之初体验(七)字符串

单个字符类型是Char。要用单引号,用了双引号就成了String。

julia> 'x'
'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)

julia> typeof(ans)
Char

julia> "p"
"p"

julia> typeof(ans)
String

Julia将转化成Unicode code值。注意要用单引号。双引号就报错,String不能转化Unicode code值。

julia> Int('x')
120

julia> typeof(ans)
Int64

julia> Int("Y")
ERROR: MethodError: no method matching Int64(::String)
Closest candidates are:
  Int64(::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) at boot.jl:707
  Int64(::Ptr) at boot.jl:717
  Int64(::Float32) at float.jl:707
  ...
Stacktrace:
 [1] top-level scope at none:0
julia> Char(120)
'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)

julia> Char(134)
'\u86': Unicode U+0086 (category Cc: Other, control)

julia> Char(233)
'é': Unicode U+00E9 (category Ll: Letter, lowercase)

julia> Char(219)
'Û': Unicode U+00DB (category Lu: Letter, uppercase)

也可以将Unicode code值转化成Char。

julia> Char(0x110000)
'\U110000': Unicode U+110000 (category In: Invalid, too high)

julia> isvalid(Char, 0x110000)
false

Julia使用系统的语言环境和语言设置来确定哪些字符可以原样打印,哪些字符必须使用通用的转义\ u或\ U输入格式输出。 除了这些Unicode转义形式之外,还可以使用C的所有传统转义输入形式:

julia> '\u0'
'\0': ASCII/Unicode U+0000 (category Cc: Other, control)

julia> '\u78'
'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)

julia> '\u2200'
'∀': Unicode U+2200 (category Sm: Symbol, math)

julia> '\U10ffff'
'\U10ffff': Unicode U+10FFFF (category Cn: Other, not assigned)
julia> Int('\0')
0

julia> Int('\t')
9

julia> Int('\n')
10

julia> Int('\e')
27

julia> Int('\x7f')
127

julia> Int('\177')
127
julia> 'A' < 'a'
true

julia> 'A' <= 'a' <= 'Z'
false

julia> 'A' <= 'X' <= 'Z'
true

julia> 'x' - 'a'
23

julia> 'A' + 1
'B': ASCII/Unicode U+0042 (category Lu: Letter, uppercase)

字符串

字符串文字用双引号或三重双引号分隔:

julia> str = "Hello, world.\n"
"Hello, world.\n"

julia> """Contains "quote" characters"""
"Contains \"quote\" characters"
julia> str[begin]
'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)

julia> str[1]
'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)

julia> str[6]
',': ASCII/Unicode U+002C (category Po: Punctuation, other)

julia> str[end]
'\n': ASCII/Unicode U+000A (category Cc: Other, control)

许多Julia对象(包括字符串)都可以用整数索引。 firstindex(str)返回第一个元素的索引(字符串的第一个字符),lastindex(str)返回最后一个元素(字符)的索引。 关键字begin和end可以在索引操作中用作沿给定维度的第一个索引和最后一个索引的简写。 像Julia中的大多数索引一样,字符串索引是基于1的:对于任何AbstractString,firstindex始终返回1。 但是,正如我们将在下面看到的那样,lastindex(str)通常与字符串的length(str)不同,因为某些Unicode字符可以占用多个“代码单元”。

julia> str[end-1]
'.': ASCII/Unicode U+002E (category Po: Punctuation, other)

julia> str[end÷2]
' ': ASCII/Unicode U+0020 (category Zs: Separator, space)

不能越界,越界就报错。

julia> str[begin-1]
ERROR: BoundsError: attempt to access String
  at index [0]
[...]

julia> str[end+1]
ERROR: BoundsError: attempt to access String
  at index [15]
[...]

字符串切片:

julia> str[4:9]
"lo, wo"
julia> str[6]
',': ASCII/Unicode U+002C (category Po: Punctuation, other)

julia> str[6:6]
","
julia> str = "long string"
"long string"

julia> substr = SubString(str, 1, 4)
"long"

julia> typeof(substr)
SubString{String}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bowen2006

你的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值