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 .\strings\string.jl:117
 [2] thisind(::String, ::Int64) at .\strings\string.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\n"
"julia\n"

julia> chomp("julia\n")
"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\nbbb")
"aaa\\nbbb"

julia> escape_string("\xfe\xff")
"\\xfe\\xff"

julia> escape_string(string('\u2135','\0'))
"ℵ\\0"

julia> escape_string(string('\u2135','\0','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"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM