Julia/字串和字元介紹
字串是由一個或多個字元組成的序列,通常用雙引號括起來。
"this is a string"
關於字串,你需要了解兩點很重要的事情。
第一,它們是不可變的。一旦建立,就不能更改它們。但是,從現有字串的部分建立新字串很容易。
第二,在使用兩個特定的字元時,要格外小心:雙引號(")和美元符號($)。如果要在字串中包含雙引號字元,則必須在它前面加上反斜槓,否則字串的其餘部分將被解釋為 Julia 程式碼,並可能產生有趣的結果。如果要在字串中包含美元符號 ($),則也應該在它前面加上反斜槓,因為它用於字串插值。
julia> demand = "You owe me \$50!" "You owe me \$50!" julia> println(demand) You owe me $50!
julia> demandquote = "He said, \"You owe me \$50!\"" "He said, \"You owe me \$50!\""
字串也可以用三個雙引號括起來。這樣做的好處是,你可以在字串中使用普通的雙引號,而無需在它們前面加上反斜槓。
julia> """this is "a" string""" "this is \"a\" string"
你還會遇到一些特殊的字串型別,它們由一個或多個字元緊跟著一個左邊的雙引號。
r" "表示正則表示式v" "表示版本字串b" "表示位元組字面量raw" "表示不會執行插值的原始字串
你經常想要在字串中使用 Julia 表示式的結果。例如,假設你想說
"The value of x is n."
其中 n 是 x 的當前值。任何 Julia 表示式都可以透過 $() 結構插入字串。
julia> x = 42 42 julia> "The value of x is $(x)." "The value of x is 42."
如果只使用變數名,則不需要使用括號。
julia> "The value of x is $x." "The value of x is 42."
要在字串中包含 Julia 表示式的結果,首先將表示式括起來,然後在前面加上美元符號。
julia> "The value of 2 + 2 is $(2 + 2)." "The value of 2 + 2 is 4."
要從字串中提取更小的字串,請使用 getindex(s, range) 或 s[range] 語法。對於基本的 ASCII 字串,你可以使用與從陣列中提取元素相同的技術。
julia> s ="a load of characters" "a load of characters" julia> s[1:end] "a load of characters" julia> s[3:6] "load"
julia> s[3:end-6] "load of char"
這等同於
julia> s[begin+2:end-6] "load of char"
你可以輕鬆地遍歷字串。
for char in s
print(char, "_")
end
a_ _l_o_a_d_ _o_f_ _c_h_a_r_a_c_t_e_r_s_
如果從字串中提取單個元素,而不是長度為 1 的字串(即具有相同的起始和結束位置),請注意。
julia> s[1:1] "a" julia> s[1] 'a'
第二個結果不是字串,而是一個字元(用單引號括起來)。
並非所有字串都是 ASCII。要訪問 Unicode 字串中的單個字元,你不能總是使用簡單的索引,因為某些字元佔據多個索引位置。不要僅僅因為某些索引號似乎有效就被誤導。
julia> su = "AéB𐅍CD"
"AéB𐅍CD"
julia> su[1]
'A'
julia> su[2]
'é'
julia> su[3]
ERROR: UnicodeError: invalid character index
in slow_utf8_next(::Array{UInt8,1}, ::UInt8, ::Int64) at ./strings/string.jl:67
in next at ./strings/string.jl:92 [inlined]
in getindex(::String, ::Int64) at ./strings/basic.jl:70
不要使用 length(str) 來查詢字串的長度,而要使用 lastindex(str)。
julia> length(su) 6
julia> lastindex(su) 10
isascii() 函式測試字串是 ASCII 還是包含 Unicode 字元。
julia> isascii(su) false
在這個字串中,"第二個"字元 é 佔 2 個位元組,"第四個"字元 𐅍 佔 4 個位元組。
for i in eachindex(su)
println(i, " -> ", su[i])
end
1 -> A 2 -> é 4 -> B 5 -> 𐅍 9 -> C 10 -> D
"第三個"字元 B 從字串中的第 4 個元素開始。
你也可以使用 pairs() 函式更輕鬆地做到這一點。
for pair in pairs(su)
println(pair)
end
1 => A 2 => é 4 => B 5 => 𐅍 9 => C 10 => D
或者,使用 eachindex 迭代器。
for charindex in eachindex(su)
@show su[charindex]
end
su[charindex] = 'A'
su[charindex] = 'é'
su[charindex] = 'B'
su[charindex] = '𐅍'
su[charindex] = 'C'
su[charindex] = 'D'
還有其他有用的函式可以處理此類字串,包括 collect()、thisind()、nextind() 和 prevind()。
julia> collect(su)
6-element Array{Char,1}:
'A'
'é'
'B'
'𐅍'
'C'
'D'
for i in 1:10
print(thisind(su, i), " ")
end
1 2 2 4 5 5 5 5 9 10
你可以使用乘法 (*) 運算子將字串貼上在一起(這個過程通常稱為連線)。
julia> "s" * "t" "st"
如果你使用過其他程式語言,你可能希望使用加法 (+) 運算子。
julia> "s" + "t" LoadError: MethodError: `+` has no method matching +(::String, ::String)
- 所以使用 *。
如果可以"乘"字串,也可以將它們取冪。
julia> "s" ^ 18 "ssssssssssssssssss"
你也可以使用 string()
julia> string("s", "t")
"st"
但是,如果你想進行大量連線,例如在迴圈中,最好使用字串緩衝區方法(見下文)。
要拆分字串,請使用 split() 函式。給定這個簡單的字串
julia> s = "You know my methods, Watson." "You know my methods, Watson."
對 split() 函式的簡單呼叫將在空格處分割字串,返回一個包含五個元素的陣列。
julia> split(s)
5-element Array{SubString{String},1}:
"You"
"know"
"my"
"methods,"
"Watson."
或者,你可以指定一個包含一個或多個字元的字串來進行拆分。
julia> split(s, "e")
2-element Array{SubString{String},1}:
"You know my m"
"thods, Watson."
julia> split(s, " m")
3-element Array{SubString{String},1}:
"You know"
"y"
"ethods, Watson."
你用來進行拆分的字元不會出現在最終結果中。
julia> split(s, "hod")
2-element Array{SubString{String},1}:
"You know my met"
"s, Watson."
如果要將字串拆分為單獨的單個字元字串,請使用空字串 (""),它會在字元之間拆分字串。
julia> split(s,"")
28-element Array{SubString{String},1}:
"Y"
"o"
"u"
" "
"k"
"n"
"o"
"w"
" "
"m"
"y"
" "
"m"
"e"
"t"
"h"
"o"
"d"
"s"
","
" "
"W"
"a"
"t"
"s"
"o"
"n"
"."
你也可以使用正則表示式來定義拆分點,從而拆分字串。使用特殊的正則表示式字串構造 r" "。在其中,你可以使用具有特殊含義的正則表示式字元。
julia> split(s, r"a|e|i|o|u")
8-element Array{SubString{String},1}:
"Y"
""
" kn"
"w my m"
"th"
"ds, W"
"ts"
"n."
這裡,r"a|e|i|o|u" 是一個正則表示式字串,如果你喜歡正則表示式,你就會知道它匹配任何母音。因此,生成的陣列包含在每個母音處分割的字串。注意結果中的空字串 - 如果你不想要這些空字串,在最後新增一個false標記。
julia> split(s, r"a|e|i|o|u", false)
7-element Array{SubString{String},1}:
"Y"
" kn"
"w my m"
"th"
"ds, W"
"ts"
"n."
如果你想保留母音,而不是將它們用於拆分工作,則必須深入瞭解正則表示式字面量字串的世界。繼續閱讀。
你可以使用 join() 將陣列形式的拆分字串的元素連線起來。
julia> join(split(s, r"a|e|i|o|u", false), "aiou") "Yaiou knaiouw my maiouthaiouds, Waioutsaioun."
Julia 中的許多函式允許你將函式用作函式呼叫的一部分。匿名函式很有用,因為你可以建立包含智慧選擇的功能的函式呼叫。例如,split() 允許你在分隔符字元的位置提供一個函式。在下一個示例中,分隔符(奇怪的是)被指定為任何 ASCII 碼是 8 的倍數的大寫字元。
julia> split(join(Char.(65:90)), c -> Int(c) % 8 == 0)
4-element Array{SubString{String},1}:
"ABCDEFG"
"IJKLMNO"
"QRSTUVW"
"YZ"
在上面,我們從更大的字串中提取了更小的字串。
julia> s[1:1] "a"
但當我們從字串中提取單個元素時
julia> s[1] 'a'
注意單引號。在 Julia 中,這些用於標記字元物件,因此 'a' 是一個字元物件,但 "a" 是一個長度為 1 的字串。它們並不等效。
你可以輕鬆地將字元物件轉換為字串。
julia> string('s') * string('d')
"sd"
或者
julia> string('s', 'd')
"sd"
使用 `\U` 轉義序列(大寫表示 32 位)輸入 32 位 Unicode 字元非常容易。小寫轉義序列 `\u` 可用於 16 位和 8 位字元。
julia> ('\U1014d', '\u2640', '\u26')
('𐅍','♀','&')
對於字串,`\Uxxxxxxxx` 和 `\uxxxx` 語法更為嚴格。
julia> "\U0001014d2\U000026402\u26402\U000000a52\u00a52\U000000352\u00352\x352" "𐅍2♀2♀2¥2¥2525252"
將整數轉換為字串是 `string()` 函式的工作。關鍵字 `base` 允許您指定轉換的數字基數,您可以使用它將十進位制數字轉換為二進位制、八進位制或十六進位制字串。
julia> string(11, base=2) "1011"
julia> string(11, base=8) "13" julia> string(11, base=16) "b" julia> string(11) "11"
julia> a = BigInt(2)^200 1606938044258990275541962092341162602522202993782792835301376
julia> string(a) "1606938044258990275541962092341162602522202993782792835301376"
julia> string(a, base=16) "1000000000000000000000000000000000000000000000000"
要將字串轉換為數字,請使用 `parse()`,您還可以指定數字基數(例如二進位制或十六進位制),如果您希望字串被解釋為使用數字基數。
julia> parse(Int, "100")
100
julia> parse(Int, "100", base=2)
4
julia> parse(Int, "100", base=16)
256
julia> parse(Float64, "100.32")
100.32
julia> parse(Complex{Float64}, "0 + 1im")
0.0 + 1.0im
`Int()` 將字元轉換為整數,`Char()` 將整數轉換為字元。
julia> Char(8253)
'‽': Unicode U+203d (category Po: Punctuation, other)
julia> Char(0x203d) # the Interrobang is Unicode U+203d in hexadecimal
'‽': Unicode U+203d (category Po: Punctuation, other)
julia> Int('‽')
8253
julia> string(Int('‽'), base=16)
"203d"
要從單個字元字串轉換為程式碼號(例如 ASCII 或 UTF 程式碼號),請嘗試以下操作
julia> Int("S"[1])
83
快速字母表
julia> string.(Char.("A"[1]:"Z"[1])) |> collect
26-element Array{String,1}:
"A"
"B"
...
"Y"
"Z"
如果您非常依賴 C 樣式的 `printf()` 功能,您將能夠使用 Julia 宏(您可以透過在宏前新增 `@` 符號來呼叫宏)。該宏在 Printf 包中提供,您需要先載入該包。
julia> using Printf
julia> @printf("pi = %0.20f", float(pi))
pi = 3.14159265358979311600
或者,您可以使用 `sprintf()` 宏建立另一個字串,該宏也位於 Printf 包中。
julia> @sprintf("pi = %0.20f", float(pi))
"pi = 3.14159265358979311600"
要從字串讀取到陣列,您可以使用 `IOBuffer()` 函式。這與許多 Julia 函式(包括 `printf()`)一起使用。這是一個數據字串(它可能已從檔案中讀取)
data="1 2 3 4
5 6 7 8
9 0 1 2"
"1 2 3 4\n5 6 7 8\n9 0 1 2"
現在,您可以使用諸如 `readdlm()` 之類的函式“讀取”此字串,即“使用分隔符讀取”函式。這可以在 DelimitedFiles 包中找到。
julia> using DelimitedFiles
julia> readdlm(IOBuffer(data))
3x4 Array{Float64,2}:
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 0.0 1.0 2.0
您可以新增可選的型別規範
julia> readdlm(IOBuffer(data), Int)
3x4 Array{Int64,2}:
1 2 3 4
5 6 7 8
9 0 1 2
有時您想對字串做一些用陣列可以更好地完成的事情。以下是一個例子。
julia> s = "/Users/me/Music/iTunes/iTunes Media/Mobile Applications";
您可以使用 `collect()` 將路徑名字串分解為字元物件陣列,`collect()` 將集合或字串中的專案收集到陣列中。
julia> collect(s)
55-element Array{Char,1}:
'/'
'U'
's'
'e'
'r'
's'
'/'
...
類似地,您可以使用 `split()` 拆分字串並計算結果。
julia> split(s, "")
55-element Array{Char,1}:
'/'
'U'
's'
'e'
'r'
's'
'/'
...
要計算特定字元物件的出現次數,您可以使用匿名函式。
julia> count(c -> c == '/', collect(s)) 6
雖然這裡轉換為陣列是多餘且效率低下的。以下是一個更好的方法
julia> count(c -> c == '/', s) 6
如果您想知道字串是否包含特定字元,請使用通用 `in()` 函式。
julia> s = "Elementary, my dear Watson";
julia> in('m', s)
true
但是 `occursin()` 函式接受兩個字串,它更常用,因為您可以使用一個或多個字元的子字串。請注意,您首先放置搜尋詞,然後放置要搜尋的字串 - `occursin(needle, haystack)`
julia> occursin("Wat", s)
true
julia> occursin("m", s)
true
julia> occursin("mi", s)
false
julia> occursin("me", s)
true
您可以使用 `findfirst(needle, haystack)` 獲取子字串首次出現的 位置。第一個引數可以是單個字元、字串或正則表示式
julia> s ="You know my methods, Watson.";
julia> findfirst("meth", s)
13:16
julia> findfirst(r"[aeiou]", s) # first vowel 2
julia> findfirst(isequal('a'), s) # first occurrence of character 'a'
23
在每種情況下,結果都包含字元的索引(如果存在)。
`replace()` 函式返回一個新的字串,其中字元的子字串被替換為其他內容
julia> replace("Sherlock Holmes", "e" => "ee")
"Sheerlock Holmees"
您使用 => 運算子指定要查詢的模式及其替換。通常第三個引數是另一個字串,如這裡所示。但是,您也可以提供一個處理結果的函式
julia> replace("Sherlock Holmes", "e" => uppercase)
"ShErlock HolmEs"
其中函式(這裡,內建的 `uppercase()` 函式)應用於匹配的子字串。
沒有 `replace!` 函式,其中“!”表示更改其引數的函式。這是因為您不能更改字串 - 它們是不可變的。
Julia 中的許多函式允許您在函式呼叫中提供函式,您可以很好地利用匿名函式來實現這一點。例如,以下是如何使用函式在 `replace()` 函式中提供隨機替換。
julia> t = "You can never foretell what any one man will do, but you can say with precision what an average number will be up to. Individuals vary, but percentages remain constant.";
julia> replace(t, r"a|e|i|o|u" => (c) -> rand(Bool) ? "0" : "1") "Y00 c1n n0v0r f1r0t1ll wh1t 0ny 0n0 m0n w1ll d0, b0t y01 c1n s1y w0th pr1c1s10n wh0t 1n 1v0r0g0 n1mb0r w0ll b0 0p t1. Ind1v0d11ls v0ry, b0t p1rc0nt0g0s r0m01n c1nst0nt."
julia> replace(t, r"a|e|i|o|u" => (c) -> rand(Bool) ? "0" : "1") "Y11 c0n...n1v0r f0r1t0ll wh1t 1ny 0n1 m0n w1ll d1, b1t y10 c1n s1y w1th pr0c1s01n wh0t 0n 0v1r0g0 n1mb1r w0ll b0 1p t1. Ind1v0d01ls v0ry, b1t p0rc1nt1g0s r0m01n c1nst0nt."
您可以使用正則表示式查詢子字串的匹配項。一些接受正則表示式的函式是
- `replace()` 更改正則表示式的出現
- `match()` 返回第一個匹配項或無
- `eachmatch()` 返回一個迭代器,讓您搜尋所有匹配項
- `split()` 在每個匹配項處拆分字串
使用 `replace()` 將每個子音替換為下劃線
julia> replace("Elementary, my dear Watson!", r"[^aeiou]" => "_")
"__e_e__a________ea___a__o__"
以下程式碼將每個母音替換為對每個匹配項執行函式的結果
julia> replace("Elementary, my dear Watson!", r"[aeiou]" => uppercase)
"ElEmEntAry, my dEAr WAtsOn!"
使用 `replace()`,您可以訪問匹配項,如果您提供一個特殊的替換字串 `s""`,其中 `\1` 指的是第一個匹配項,`\2` 指的是第二個匹配項,依此類推。使用此正則表示式操作,每個以空格開頭的 小寫字母都會重複三次
julia> replace("Elementary, my dear Watson!", r"(\s)([a-z])" => s"\1\2\2\2")
"Elementary, mmmy dddear Watson!"
有關更多正則表示式樂趣,請使用 `-match-` 函式。
這裡我從檔案中載入了“福爾摩斯探案集”的完整文字到名為 `text` 的字串中。
julia> f = "/tmp/adventures-of-sherlock-holmes.txt" julia> text = read(f, String);
要將匹配的可能性用作布林條件,例如適合在 `if` 語句中使用,請使用 `occursin()`。
julia> occursin(r"Opium", text) false
這很奇怪。我們期望找到這位偉大偵探的特殊藥物消遣的證據。事實上,“鴉片”一詞確實出現在文字中,但只出現在小寫中,因此產生了這個 `false` 結果——正則表示式區分大小寫。
julia> occursin(r"(?i)Opium", text) true
這是一個不區分大小寫的搜尋,由標誌 `(?i)` 設定,它返回 `true`。
您可以使用一個簡單的迴圈檢查每一行中是否有該詞。
for l in split(text, "\n")
occursin(r"opium", l) && println(l)
end
opium. The habit grew upon him, as I understand, from some
he had, when the fit was on him, made use of an opium den in the
brown opium smoke, and terraced with wooden berths, like the
wrinkled, bent with age, an opium pipe dangling down from between
very short time a decrepit figure had emerged from the opium den,
opium-smoking to cocaine injections, and all the other little
steps - for the house was none other than the opium den in which
lives upon the second floor of the opium den, and who was
learn to have been the lodger at the opium den, and to have been
doing in the opium den, what happened to him when there, where is
"Had he ever showed any signs of having taken opium?"
room above the opium den when I looked out of my window and saw,為了獲得更易用的輸出(在 REPL 中),請新增 `enumerate()` 和一些突出顯示。
red = Base.text_colors[:red]; default = Base.text_colors[:default];
for (n, l) in enumerate(split(text, "\n"))
occursin(r"opium", l) && println("$n $(replace(l, "opium" => "$(red)opium$(default)"))")
end
5087 opium. The habit grew upon him, as I understand, from some 5140 he had, when the fit was on him, made use of an opium den in the 5173 brown opium smoke, and terraced with wooden berths, like the 5237 wrinkled, bent with age, an opium pipe dangling down from between 5273 very short time a decrepit figure had emerged from the opium den, 5280 opium-smoking to cocaine injections, and all the other little 5429 steps - for the house was none other than the opium den in which 5486 lives upon the second floor of the opium den, and who was 5510 learn to have been the lodger at the opium den, and to have been 5593 doing in the opium den, what happened to him when there, where is 5846 "Had he ever showed any signs of having taken opium?" 6129 room above the opium den when I looked out of my window and saw,
新增正則表示式修飾符(如不區分大小寫的匹配)有另一種語法。請注意第二個示例中正則表示式字串後面的“i”。
julia> occursin(r"Opium", text) false julia> occursin(r"Opium"i, text) true
使用 `eachmatch()` 函式,您將正則表示式應用於字串以生成迭代器。例如,在我們的文字中查詢匹配字母“L”,後面是其他一些字元,以“ed”結尾的子字串。
julia> lmatch = eachmatch(r"L.*?ed", text)
`lmatch` 中的結果是一個可迭代物件,包含所有匹配項,作為 RegexMatch 物件。
julia> collect(lmatch)[1:10]
10-element Array{RegexMatch,1}:
RegexMatch("London, and proceed")
RegexMatch("London is a pleasant thing indeed")
RegexMatch("Looking for lodgings,\" I answered")
RegexMatch("London he had received")
RegexMatch("Lied")
RegexMatch("Life,\" and it attempted")
RegexMatch("Lauriston Gardens wore an ill-omened")
RegexMatch("Let\" card had developed")
RegexMatch("Lestrade, is here. I had relied")
RegexMatch("Lestrade grabbed")
我們可以遍歷迭代器並依次檢視每個匹配項。您可以訪問 RegexMatch 的許多欄位,以提取有關匹配項的資訊。其中包括 `captures`、`match`、`offset`、`offsets` 和 `regex`。例如,`match` 欄位包含匹配的子字串。
for i in lmatch
println(i.match)
end
London - quite so! Your Majesty, as I understand, became entangled
Lodge. As it pulled
Lord, Mr. Wilson, that I was a red
League of the Red
League was founded
London when he was young, and he wanted
LSON" in white letters, upon a corner house, announced
League, and the copying of the 'Encyclopaed
Leadenhall Street Post Office, to be left till called
Let the whole incident be a sealed
Lestrade, being rather puzzled
Lestrade would have noted
...
Lestrade," drawled
Lestrade looked
Lord St. Simon has not already arrived
Lord St. Simon sank into a chair and passed
Lord St. Simon had by no means relaxed
Lordship. "I may be forced
London. What could have happened
London, and I had placed其他欄位包括 `captures`,捕獲的子字串作為字串陣列,`offset`,整個匹配開始時字串中的偏移量,以及 `offsets`,捕獲的子字串的偏移量。
要獲取匹配字串的陣列,請使用類似以下的內容
julia> collect(m.match for m in eachmatch(r"L.*?ed", text))
58-element Array{SubString{String},1}:
"London - quite so! Your Majesty, as I understand, became entangled"
"Lodge. As it pulled"
"Lord, Mr. Wilson, that I was a red"
"League of the Red"
"League was founded"
"London when he was young, and he wanted"
"Leadenhall Street Post Office, to be left till called"
"Let the whole incident be a sealed"
"Lestrade, being rather puzzled"
"Lestrade would have noted"
"Lestrade looked"
"Lestrade laughed"
"Lestrade shrugged"
"Lestrade called"
...
"Lord St. Simon shrugged"
"Lady St. Simon was decoyed"
"Lestrade,\" drawled"
"Lestrade looked"
"Lord St. Simon has not already arrived"
"Lord St. Simon sank into a chair and passed"
"Lord St. Simon had by no means relaxed"
"Lordship. \"I may be forced"
"London. What could have happened"
"London, and I had placed"
基本 `match()` 函式查詢正則表示式的第一個匹配項。使用 `match` 欄位從 RegexMatch 物件中提取資訊。
julia> match(r"She.*",text).match "Sherlock Holmes she is always THE woman. I have seldom heard\r"
從檔案中獲取匹配行的更簡潔的方法是
julia> f = "adventures of sherlock holmes.txt"
julia> filter(s -> occursin(r"(?i)Opium", s), map(chomp, readlines(open(f))))
12-element Array{SubString{String},1}:
"opium. The habit grew upon him, as I understand, from some"
"he had, when the fit was on him, made use of an opium den in the"
"brown opium smoke, and terraced with wooden berths, like the"
"wrinkled, bent with age, an opium pipe dangling down from between"
"very short time a decrepit figure had emerged from the opium den,"
"opium-smoking to cocaine injections, and all the other little"
"steps - for the house was none other than the opium den in which"
"lives upon the second floor of the opium den, and who was"
"learn to have been the lodger at the opium den, and to have been"
"doing in the opium den, what happened to him when there, where is"
"\"Had he ever showed any signs of having taken opium?\""
"room above the opium den when I looked out of my window and saw,"
有時您想從程式碼中建立正則表示式。您可以透過建立 Regex 物件來實現。以下是如何在文字中計算母音數量的一種方法
f = open("sherlock-holmes.txt")
text = read(f, String)
for vowel in "aeiou"
r = Regex(string(vowel))
l = [m.match for m = eachmatch(r, text)]
println("there are $(length(l)) letter \"$vowel\"s in the text.")
end
there are 219626 letter "a"s in the text. there are 337212 letter "e"s in the text. there are 167552 letter "i"s in the text. there are 212834 letter "o"s in the text. there are 82924 letter "u"s in the text.
有時您需要組裝替換字串。為此,您可以使用 `SubstitutionString()` 而不是 `s"..."`。
例如,假設您想在替換字串中進行一些字串插值。也許您有一列檔案,您想重新編號它們,以便“file2.png”變為“file1.png”。
files = ["file2.png", "file3.png", "file4.png", "file5.png", "file6.png", "file7.png"]
for (n, f) in enumerate(files)
newfilename = replace(f, r"(.*)\d\.png" => SubstitutionString("\\g<1>$(n).png"))
# now to do the renaming...
請注意,您不能簡單地在 SubstitutionString 中使用 `\1` 來引用第一個捕獲的表示式,您必須將其轉義為 `\\1`,並使用 `\g`(轉義為 `\\g`)來引用命名的捕獲組。
有很多函式可以用來測試和更改字串。
length(str)字串的長度sizeof(str)長度/大小startswith(strA, strB)strA 是否以 strB 開頭?endswith(strA, strB)strA 是否以 strB 結尾?occursin(strA, strB)strA 是否出現在 strB 中?all(isletter, str)str 是否完全由字母組成?all(isnumeric, str)str 是否完全由數字字元組成?isascii(str)str 是否為 ASCII 字串?all(iscntrl, str)str 是否完全由控制字元組成?all(isdigit, str)str 是否為 0-9?all(ispunct, str)str 是否由標點符號組成?all(isspace, str)str 是否為空白字元?all(isuppercase, str)str 是否為大寫?all(islowercase, str)str 是否完全為小寫?all(isxdigit, str)str 是否完全由十六進位制數字組成?uppercase(str)返回 str 的大寫副本lowercase(str)返回 str 的小寫副本titlecase(str)返回 str 的副本,其中每個單詞的第一個字元都轉換為大寫uppercasefirst(str)返回 str 的副本,其中第一個字元轉換為大寫lowercasefirst(str)返回 str 的副本,其中第一個字元轉換為小寫chop(str)返回移除最後一個字元的副本chomp(str)返回移除最後一個字元的副本,但只有當該字元為換行符時
要寫入字串,可以使用 Julia 流。sprint()(字串列印)函式允許您使用函式作為第一個引數,並使用該函式和其他引數向流傳送資訊,並將結果作為字串返回。
例如,考慮以下函式 f。函式的主體將一個匿名“列印”函式對映到引數上,並將它們用尖括號括起來。當被 sprint 使用時,函式 f 處理其餘引數並將它們傳送到流。
function f(io::IO, args...)
map((a) -> print(io,"<",a, ">"), args)
end
f (generic function with 1 method)
julia> sprint(f, "fred", "jim", "bill", "fred blogs") "<fred><jim><bill><fred blogs>"
像 println() 這樣的函式可以將 IOBuffer 或流作為其第一個引數。這使您可以將內容列印到流而不是列印到標準輸出裝置。
julia> iobuffer = IOBuffer() IOBuffer(data=Uint8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)
julia> for i in 1:100
println(iobuffer, string(i))
end
之後,名為 iobuffer 的記憶體中流將充滿了數字和換行符,即使終端上沒有列印任何內容。要將 iobuffer 中的內容從流複製到字串或陣列,可以使用 take!()
julia> String(take!(iobuffer)) "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14 ... \n98\n99\n100\n"
以下使用 printstyled 打印出相應的顏色訊息。
julia> for color in [:red, :green, :blue, :magenta]
printstyled("Hello in $(color)\n"; color = color)
end
Hello in red
Hello in green
Hello in blue
Hello in magenta
在 try catch 語句中間,以下程式碼將列印導致異常的原始回溯。
try
# some code that can fail, but you want to continue even after a failure
catch e
# show the error, but with its backtrace
showerror(stderr, e, catch_backtrace())
end
如果您不在 try-catch 中,並且想要在不停止執行的情況下列印堆疊跟蹤,請使用以下程式碼。
showerror(stderr, ErrorException("show stacktrace"), stacktrace())