Comprehensive Summary of Julia String Syntax and Functions
Overview
This document provides a brief overview of the syntax and functions related to characters/strings used in Julia. The version reference is Julia v1.11.5.
Official Documentation
- Manual>Strings: https://docs.julialang.org/en/v1/manual/strings/
- Base>Strings: https://docs.julialang.org/en/v1/base/strings/
Summary
- Definition
'x'
"x"
repr(foo)
Char(decimal)
Char(hex)
- Operations
"foo" < "bar"
"foo" > "bar"
"foo" == "bar"
"foo" != "bar"
isless("foo", "bar")
- Predicates
isspace(' ')
isletter('x')
isuppercase('X')
islowercase('x')
isdigit('1')
isxdigit('x')
isnumeric('1')
ispunct('!')
isascii("foo")
iscntrl('x')
isprint('x')
- General
length("foo bar")
sizeof("foo bar")
"foo bar"[1]
"foo bar"[1:3]
first("foo bar", n)
last("foo bar", n)
"foo" * "bar"
=string("foo", "bar")
"foo"^n
=repeat("foo", n)
join(["foo1", "foo2"], "bar")
split("foo bar", " ")
"foo $bar"
- Transformation
replace("foo bar", "o"=>"x")
reverse("foo bar")
lowercase("FOO BAR")
lowercasefirst("FOO BAR")
uppercase("foo bar")
uppercasefirst("foo bar")
titlecase("foo bar")
- Insertion/Removal
chop("foo bar", head=n, tail=m)
chopprefix("foo", "bar")
chopsuffix("foo", "bar")
strip(" foo bar ")
lstrip(" foo bar ")
rstrip(" foo bar ")
lpad("foo", n)
rpad("foo", n)
filter(function, "foo bar")
- Search
contains(haystack, needle)
occursin(needle, haystack)
startswith(haystack, needle)
endswith(haystack, needle)
findfirst("foo", "bar")
findlast("foo", "bar")
findnext("foo", "bar", n)
findprev("foo", "bar", n)
findall(function, haystack)
Definition
Syntax | Code |
'x' | #Define a character (Char) with single quotes (') julia> 'x' 'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase) julia> 'xx' ERROR: ParseError: julia> typeof('x') Char |
"x" | #Define a string (String) with double quotes (") julia> "x" "x" julia> typeof("x") String |
repr(foo) | #Return string representation julia> repr(123) "123" julia> repr(zeros(3)) "[0.0, 0.0, 0.0]" |
Char(decimal) Char(hex) | #Convert decimal/hexadecimal to Unicode character julia> Char(0xAC00), Char(44032) ('가', '가') |
Operations
Syntax | Code |
"foo" < "bar" "foo" > "bar" | #Unicode comparison of the first character julia> Int('W'), Int('Y') (87, 89) julia> "Wonyoung" < "Yujin" true julia> "Wonyoung" > "Yujin" false |
"foo" == "bar" "foo" != "bar" | #Comparison of two strings julia> "Wonyoung" == "wonyoung" false julia> "Wonyoung" == "Wonyoung" true julia> "Wonyoung" != "Wonyoung" false julia> "Wonyoung" != "wonyoung" true |
isless("foo", "bar") | #Determine if "foo" is less than "bar" in Unicode julia> [Char('1'), Char('a'), Char('가'), Char('나')] 4-element Vector{Char}: '1': ASCII/Unicode U+0031 (category Nd: Number, decimal digit) 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase) '가': Unicode U+AC00 (category Lo: Letter, other) '나': Unicode U+B098 (category Lo: Letter, other) julia> isless('1', 'a'), isless('a', '가'), isless('나', '가') (true, true, false) |
Predicates
Syntax | Code |
isspace(' ') | #Determine if it is a whitespace character (space, newline, tab) julia> isspace(' '), isspace('\n'), isspace('\t') (true, true, true) julia> isspace('x') false |
isletter('x') | #Determine if it is a Unicode category letter (Lu, Ll, Lt, Lm, Lo) julia> isletter('j'), isletter('θ'), isletter('가'), isletter('あ') (true, true, true, true) julia> isletter('1'), isletter('?'), isletter('😊'), isletter(' ') (false, false, false, false) |
isuppercase('X') islowercase('x') | #Determine if it is uppercase or lowercase julia> isuppercase('j'), isuppercase('J') (false, true) julia> islowercase('j'), islowercase('J') (true, false) |
isdigit('1') isxdigit('x') | #Determine if it is a decimal or hexadecimal digit julia> isdigit('1'), isdigit('a'), isdigit('F'), isdigit('x') (true, false, false, false) julia> isxdigit('1'), isxdigit('a'), isxdigit('F'), isxdigit('x') (true, true, true, false) |
isnumeric('1') | #Determine if it is a numeric Unicode category (Nd, Nl, No) julia> isnumeric('1'), isnumeric('Ⅳ'), isnumeric('½'), isnumeric('௰') (true, true, true, true) julia> isnumeric('a'), isnumeric('가'), isnumeric('😊'), isnumeric('?') (false, false, false, false) |
ispunct('!') | #Determine if it is a punctuation mark julia> ispunct('!'), ispunct('.'), ispunct('/'), ispunct('?') (true, true, true, true) julia> ispunct('1'), ispunct('x'), ispunct('가'), ispunct('😊') (false, false, false, false) |
isascii("foo") | #Determine if it is an ASCII code julia> isascii("θ"), isascii("xθ"), isascii("123θ") (false, false, false) julia> isascii("x"), isascii("xyz"), isascii("123"), isascii("[") (true, true, true, true) |
iscntrl('x') | #Determine if it is a control character julia> iscntrl('x'), iscntrl('1'), iscntrl('\n'), iscntrl('\t') (false, false, true, true) |
isprint('x') | #Determine if it is a printable character julia> isprint('x'), isprint('1'), isprint('\n'), isprint('\t') (true, true, false, false) |
General
Function/Syntax | Code |
length("foo bar") | #Return the length of the string julia> length("Jang Wonyoung") 13 |
sizeof("foo bar") | #Return the byte size of the string julia> sizeof("xyz_. ?"), length("xyz_. ?") (7, 7) julia> sizeof("αβγ"), length("αβγ") (6, 3) julia> sizeof("😊😂"), length("😊😂") (8, 2) |
"foo bar"[1] "foo bar"[1:3] | #String indexing, slicing julia> wonyo = "Jang Wonyoung" "Jang Wonyoung" julia> wonyo[1] 'J': ASCII/Unicode U+004A (category Lu: Letter, uppercase) julia> wonyo[6:end] "Wonyoung" |
first("foo bar", n) last("foo bar", n) | #Slice the first/last n characters of the string julia> first("Jang Wonyoung", 3) "Jan" julia> last("Jang Wonyoung", 5) "young" |
"foo" * "bar" | #Concatenate strings, equivalent to string("foo", "bar") julia> "Jang" * "Wonyoung" "JangWonyoung" |
string("foo", "bar") | #Concatenate strings, equivalent to "foo" * "bar" julia> string("Jang", "Wonyoung") "JangWonyoung" |
"foo"^n | #Repeat string concatenation, equivalent to repeat("foo", n) julia> "Tung"^9 "TungTungTungTungTungTungTungTungTung" |
repeat("foo", n) | #Repeat string concatenation, equivalent to "foo"^n julia> repeat("Tung", 9) "TungTungTungTungTungTungTungTungTung" |
join(["foo1", "foo2"], "bar") | #Join a string array with a specific separator julia> join(["Jang Wonyoung", "Karina", "Sullyoon"], " and ") "Jang Wonyoung and Karina and Sullyoon" |
split("foo bar", " ") | #Split the string by a specific separator julia> split("Jang Wonyoung, Karina, Sullyoon", ", ") 3-element Vector{SubString{String}}: "Jang Wonyoung" "Karina" "Sullyoon" |
"foo $bar" | #String interpolation julia> name = "Wonyoung" "Wonyoung" julia> "Jang $name" "Jang Wonyoung" julia> "1 + 2 = 3" == "1 + 2 = $(1 + 2)" true |
Transformation
Function | Code |
replace("foo bar", "o"=>"x") | #Replace specific characters in a string julia> replace("izone", "zon"=>"v") "ive" julia> replace("izone", "z"=>"v", "o"=>"", "n"=>"") "ive" |
reverse("foo bar") | #Reverse a string julia> reverse("Jang Wonyoung") "gnuoynoW gnaJ" |
lowercase("FOO BAR") | #Convert string to lowercase julia> lowercase("JANG WONYOUNG") "jang wonyoung" |
lowercasefirst("FOO BAR") | #Lowercase the first character julia> lowercasefirst("JANG WONYOUNG") "jANG WONYOUNG" |
uppercase("foo bar") | #Convert string to uppercase julia> uppercase("jang wonyoung") "JANG WONYOUNG" |
uppercasefirst("foo bar") | #Uppercase the first character julia> uppercasefirst("jang wonyoung") "Jang wonyoung" |
titlecase("foo bar") | #Uppercase the first character of each word julia> titlecase("jang wonyoung") "Jang Wonyoung" |
Insertion/Removal
Function | Code |
chop("foo bar", head=n, tail=m) | #Remove n characters from the start and m characters from the end of a string julia> chop("Jang Wonyoung", head=3, tail=5) "g Won" |
chopprefix("foo", "bar") chopsuffix("foo", "bar") | #Remove if the string starts/ends with "bar" julia> chopprefix.(["Jang Wonyoung", "An Yujin"], "Jang ") 2-element Vector{SubString{String}}: "Wonyoung" "An Yujin" julia> chopsuffix.(["Jang Wonyoung", "An Yujin"], "Wonyoung") 2-element Vector{SubString{String}}: "Jang " "An Yujin" |
strip(" foo bar ") | #Remove whitespace or specific characters from both ends of a string julia> strip(" Julia Language ") "Julia Language" julia> strip(" Julia Language___", [' ', '_']) "Julia Language" |
lstrip(" foo bar ") rstrip(" foo bar ") | #Remove whitespace (or specific characters) from the left/right end of a string julia> lstrip(" Julia Language ") "Julia Language " julia> rstrip(" Julia Language___", [' ', '_']) " Julia Language" |
lpad("foo", n) rpad("foo", n, '_') | #Extend string length to n and add padding on the left/right with space (or specific character) julia> lpad("xyz", 10) " xyz" julia> rpad("xyz", 10, '_') "xyz_______" |
filter(function, "foo bar") | #Keep only characters in a string that satisfy the function julia> filter(isuppercase, "Jang Wonyoung") "JW" julia> filter(isspace, "Jang Wonyoung") " " julia> filter(!isspace, "Jang Wonyoung") "JangWonyoung" |
Search
Function | Code |
contains(haystack, needle) | #Check if haystack contains needle julia> contains("Jang Wonyoung", "y") true julia> contains("Jang Wonyoung", "x") false |
occursin(needle, haystack) | #Check if haystack contains needle julia> occursin("y", "Jang Wonyoung") true julia> occursin("x", "Jang Wonyoung") false |
startswith(haystack, needle) | #Check if haystack starts with needle julia> startswith("hello", "he") true |
endswith(haystack, needle) | #Check if haystack ends with needle julia> endswith("hello", "lo") true |
findfirst("foo", "bar") | #Find the position of a specific character in a string (from left) julia> findfirst('g', "Jang wonyoung") 4 julia> findfirst("ng", "Jang wonyoung") 3:4 |
findlast("foo", "bar") | #Find the position of a specific character in a string (from right) julia> findlast('g', "Jang wonyoung") 13 julia> findlast("ng", "Jang wonyoung") 12:13 |
findnext("foo", "bar", n) | #Find the position of a specific character in a string (search only after the nth position) julia> findnext('g', "Jang wonyoung", 2) 4 julia> findnext('g', "Jang wonyoung", 6) 13 |
findprev("foo", "bar", n) | #Find the position of a specific character in a string (search only before the nth position) julia> findprev('o', "Jang wonyoung", 13) 10 julia> findprev('o', "Jang wonyoung", 9) 7 |
findall(function, haystack) | #Find all indices in haystack that satisfy the function julia> findall(isuppercase, "Jang Wonyoung") 2-element Vector{Int64}: 1 6 julia> findall(isspace, "Jang Wonyoung") 1-element Vector{Int64}: 5 julia> findall(!isspace, "Jang Wonyoung") 12-element Vector{Int64}: 1 2 3 4 6 7 8 9 10 11 12 13 |