Char字符String字符串 2020/5/31
==================================================================================
1.1.Char:
# Char 代表单字符,由32位整数表示 Unicode 码位;必须使用单引号
# 子类型AbstractChar是32位原始类型可表示任何Unicode字符(并且基于UTF-8编码)
1.2.构造字符:
'x',Char(120),Char('x') #ASCII构造:('x', 'x', 'x')
# 用系统默认区域和语言设置来确定哪些字符可被显示,哪些需用 \u 或 \U 转义来显示
'中' # '中': Unicode U+4E2D (category Lo: Letter, other)
'\u78', '\u2200' # Unicode 码构造: ('x', '∀') 最多4位十六进制数
'\U000078','\U002200' # Unicode 码构造: ('x', '∀') 最多6位十六进制数
===================================================================================
1.3.Char 转整数值:
int('x') # 120
Int('\0'),Int('\t'),Int('\n'),Int('\e'),Int('\x7f'),Int('\177')# C 转义字符:(0, 9, 10, 27, 127, 127)
===================================================================================
1.4.Char运算:
'A' < 'a'<'z' # Char 值比较大小:true
'x' - 1,'x' - 'a', 'A' + 1 # Char值加减运算:('w', 23, 'B')
Char(0x110000),isvalid(Char, 0x110000)# 检测字符是否有效:('\U110000', false)
===================================================================================
2.字符串
2.1.String
# 字符串类型String不可更改;支持 ASCII 全部Unicode(通常 UTF-8也支持其它编码)
# 如定义函数需字符串参数应将类型声明为AbstractString以便接受任何字符串类型
# 字符串是从索引值映射到字符的部分函数,对某些索引值,如果不是字符,会抛出异常
# string抽象非具体类型
2.1.定义:字符串应用双引号 "..." 或三双引号 """..."""
2.2.构造:
"Hello, world.\n","""Hello, world.\n"""#("Hello, world.\n", "Hello, world.\n")
2.3.索引:
str="Hello, world.\n" # Julia 索引整数[1 , n ];<1 或>end 错误
str[6],str[6:6] # (',', ",")
str[0],str[end+1] # 都错误
str[firstindex(str)]==str[begin],str[lastindex(str)]==str[end] # str首尾索引:(true, true)
str[begin*2],str[begin+1],str[end-1] ,str[end÷2] ,str[div(end,2)] # 字符串运算:('e', 'e', '.', ' ', ' ')
str[begin],str[6],str[end],str[4:9] # 提取字符:('H', ',', '\n', "lo, wo")
=====================================================================================
2.4.字符串操作
2.4.1字符串连接:
str1 ,str2= "Hello","Hello"
string(str1, ", ", str2, ".\n"),str1 * ", " * str2 * ".\n" # ("Hello, Hello.\n", "Hello, Hello.\n")
2.4.2.内插:文本表达式字符数组
c,name,age,arr='!',"Tom",(1+20),[1,2,3]
"Name=$name;age=$age $c arr=$arr\n" # "Name=Tom;age=21 ! arr=[1, 2, 3]\n"必须全是字符串
String("Name=$name;age=$age $c arr=$arr\n") # "Name=Tom;age=21 ! arr=[1, 2, 3]\n"必须全是字符串
string("Name=$name;age=$age $c"," arr=",arr,"\n") # "Name=Tom;age=21 ! arr=[1, 2, 3]\n"类似python print格式
print(" \$100 \n") # $100 \$转义
2.4.3.字符串比较:<,>,==,!=
"1 + 2 = 3" == "1 + 2 = $(1 + 2)" # 字符串比较:true
2.4.4.字符的索引值:
str="xylophone"
search("xylophone", 'x') #查找字符索引:1,找不到返回0
search("xylophone", 'o', 5)#从此偏移值开始查找:7
findfirst(isequal('o'),str),findlast(isequal('o'), str) #首尾索引:(4, 7)
findnext(isequal('o'), str, 1),findnext(isequal('o'), str, 5)#指定偏移:(4, 7)
findprev(isequal('o'), str, 5)#指定偏移:4
SubString(str, 1, 4) # 取子串
occursin("phone", str),occursin('o', str)# 是否存在子串:(true, true)
repeat("x1:", 2) # 重复字符串次数:"x1:x1:"
join(["x1,", "x3"], ", ","x2,") # "x1,x2,x3"
2.4.5.其他函数:
length(str) str中的字符数str。
length(str, i, j) str从i到的有效字符索引数j
ncodeunits(str) 字符串中的代码单元数
codeunit(str, i) str在index处的字符串中给出代码单位值i。
thisind(str, i) 给定任意索引到字符串中,找到索引指向的字符的第一个索引。
nextind(str, i, n=1) n在index之后找到第一个字符的开头i。
prevind(str, i, n=1) n在索引index之前找到第一个字符的开头i
=====================================================================================
3.备注:
3.1.Unicode 和 UTF-8
# 非 ASCII 字符串用 UTF-8 编码(变长编码;<0x80 (128) 字符 ASCII 用单字节)支持 UTF-16 和 UTF-32 编码;
# 其余码位字符用多字节(max四字节)并非所有字节索引值都有效;无效字节索引值抛错误
实例1:获取字符串的有效索引
s = "\u2200 x \u2203 y" # "∀ x ∃ y"
length(s) #7
s[2],s[3] #错误:无效字节索引
for i = firstindex(s):lastindex(s)
try println(s[i]) catch ; end
end
for c in s println(c) end
show([s[i] for i in eachindex(s)]) # ['∀', ' ', 'x', ' ', '∃', ' ', 'y']
show(collect(eachindex(s))) # [1, 4, 5, 6, 7, 10, 11]
function get_str(s)
a=[i==1 ? 1 : nextind(s,i) for i in 1:length(s)]# nextind,prevind递增/减到下/上一个有效索引
set=Set(a);a=[v for v in set];sort!(a) # ['∀', ' ', 'x', ' ', '∃', ' ']
show([s[i] for i in a])
end
3.2.实例2:字符串连接问题
a, b = "\xe2\x88", "\x80"
c = a*b # "∀" 无效UTF-8str串联:str可能与输入str不同且字符数可能少于串联字符串的字符数之和
collect.([a, b, c])
# 3-element Array{Array{Char,1},1}:
# ['\xe2\x88']
# ['\x80']
# ['∀']
========================================================================================
3.3.三重引用的字符串文字
# 三引号("""...""")创建字符串对创建更长的文本块很有用。
字符串文字可包含"不带转义符符号。
注意:
\n即使编辑器用回车\r(CR)或CRLF组合作为结束行,文字字符串换行符(无论是单引号还是三引号)
都会在字符串中产生换行符(LF)。
要在字符串中包含CR,请使用显式转义\r; 如输入文字字符串"a CRLF line ending\r\n"
实例1:
str = """
Hello,
world.
"""
# " Hello,\n world.\n"
实例2:
""" This
is
a test"""
# " This\nis\n a test" 仅仅保留This前的空格
实例3.1:
"""hello"""
# "hello"
实例3.2.:
"""
hello"""
# "hello" 等价上面
实例4:
"""
hello"""
# "\nhello"开头将包含文字换行符
实例5:
"""
Hello,
world."""
# "Hello,\nworld."
========================================================================================