
生信探索
V1
2023/04/17阅读:24主题:姹紫
Julia编程06:String与Symbol
<~生~信~交~流~与~合~作~请~关~注~公~众~号@生信探索>
字符与字符串
Julia是区分字符和字符串的,而且都是不可修改的类型
typeof('A') == Char
typeof("A") == String
遍历
julia的字符串索引是根据bytes排,非ASCII码的字符可能由多个bytes编码,比如汉
占了3个bytes,所以字
的索引是4
for i in "汉字123Abc"
println(i)
end
# 汉
# 字
# 1
# 2
# 3
# A
# b
# c
for i in eachindex("汉字123Abc")
println(i)
end
# 1
# 4
# 7
# 8
# 9
# 10
# 11
# 12
"汉字123Abc"[2]
# StringIndexError: invalid index [2], valid nearby indices [1]=>'汉', [4]=>'字'
codeunits("汉")
# 3-element Base.CodeUnits{UInt8, String}:
# 0xe6
# 0xb1
# 0x89
为了避免出现上述的索引错误,安全的方法是使用内置函数
length("汉字123Abc")== 8
#前3个字符
first("汉字123Abc",3)=="汉字1"
#后3个字符
last("汉字123Abc",3)=="Abc"
##前1个后2字符
chop("汉字123Abc",head=1,tail=2)=="字bc"
# 返回s中第一个字符的下标
firstindex("汉字123Abc") == 1
# 返回s中最后一个字符的下标
lastindex("汉字123Abc") == 1
# 前3个bytes对应字符的索引都是1即`汉`
thisind("汉字123Abc", 1) == thisind("汉字123Abc", 2) == thisind("汉字123Abc", 3) == 1
# 第4个byte对应字符的索引4即`字`
thisind("汉字123Abc", 4) == 4
nextind("汉字123Abc",1, 3)
-
判断字符串是不是仅由ASCII码组成
isascii("汉字123Abc") == false
isascii("123Abc") == true
methods
-
字符串拼接
区别与Python的字符拼接,Julia使用的是 *
,而Python使用的是 +
"abc" * "def" # "abcdef"
join([1,"B",3], '-') # "1-B-3"
str_list=["hello","world","julians!"]
string(str_list...) # "helloworldjulians!"
string("AP", "P", "LE") #"APPLE"
-
字符串插值
这个类似与shell中的字符串差值
name = "Victor"
"hello world $name" # "hello world Victor"
x=[1,2,3]
"the sum of $x is $(sum(x))" #"the sum of [1, 2, 3] is 6"
-
raw
保留字符串中的escape,跟python中的r""类似
str = raw"""
Hello \n
world
""" # "Hello \\n\nworld\n"
-
类型转换
# 将字符串s中的数值转换成整数类型
parse(Int64, "9") == 9
# 将字符串s中的数值转换成Float64类型
parse(Float64, "9") == 9.0
# 将表达式x的值表示成字符串
repr(9) == "9" #
# string to vector
collect("汉字123Abc")
# 8-element Vector{Char}:
# '汉': Unicode U+6C49 (category Lo: Letter, other)
# '字': Unicode U+5B57 (category Lo: Letter, other)
# '1': ASCII/Unicode U+0031 (category Nd: Number, decimal digit)
# '2': ASCII/Unicode U+0032 (category Nd: Number, decimal digit)
# '3': ASCII/Unicode U+0033 (category Nd: Number, decimal digit)
# 'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
# 'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
# 'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
-
字符串分割
split("1,2,3", ",")
查找
findfirst("ing", "Begining things") # 6:8
findnext("ing", "Begining things", 9) # 12:14 找到某个模式在指定位置之后的首次出现
findprev # 找到某个模式在指定位置之前的首次出现
startswith("Photo", "Ph")
endswith("weight", "ght")
occursin("oar", "board")
正则表达式
s = "TCGA-02-0001-01C-01D-0182-01"
ptn = r"01[CD]"i # i 不区分大小写
occursin(ptn, s) # true
m = match(ptn, s)
m.match # return 匹配到的字符串 # "TCGA-02-0001-01"
m.offset # 开始字符下标
m.offsets # 匹配的各个子模式的开始字符下标
m.captures # 返回匹配的各个子模式
replced = s"\1ONE\2"
replace(s,ptn=>replced) #
# ------------------多处匹配----------------
eachmatch(ptn, s)
for imatch in eachmatch(ptn, s)
println("\"$(imatch.match)\"")
end
固定长度字符串
InlineStrings.jl 包提供6种长度的字符串类型。
优点:可以提高运行速度,节约内存;
使用场景:固定长度的字符串,比如TCGA的barcode
CSV包读取数据的时候会自动判断是否应该使用固定长度字符串
InlineStrings可以自动判断使用多少长度,一般不需自己指定
String1—Size up to 1
byteString3—Size up to 3 bytes
String7—Size up to 7 bytes
String15—Size up to 15 bytes
String31—Size up to 31 bytes
String63—Size up to 63 bytes
String127—Size up to 127 bytes
String255—Size up to 255 bytes
import Pkg; Pkg.add("InlineStrings")
using InlineStrings
inlinestrings(["red","green","pink","yellow"])
# 4-element Vector{String7}:
# "red"
# "green"
# "pink"
# "yellow"
Symbol特点
Symbol和字符串类似,但是更节省内存,更快速
因为在整个global环境中所有Symbol都存在于Symbol pool中,因为Symbol是不可变类型,因此1000个:red
也只在内存中存一次,所有:red都指向同一个内存地址。
创建Symbol
两种写法都可以,但是当想要创建的Symbol不是合法的变量名时,只能用第一种方法
Symbol("red") == :red
typeof(:1314) == Int64
# 只能写做
Symbol(1314) == Symbol("1314")
:red color
# syntax: extra token "color" after end of expression
Symbol("red color") == Symbol("red color")
Symbol("red","color") == :redcolor
supertype(Symbol) == Any
作者介绍

生信探索
V1
微信公众号:生信探索