logo

줄리아 문자열 관련 문법 및 함수 총 정리 📂줄리아

줄리아 문자열 관련 문법 및 함수 총 정리

개요

줄리아에서 쓰이는 문자/문자열에 관한 문법 및 함수를 간략히 정리한다. 기준은 줄리아 버전 v1.11.5이다.

공식문서

요약

  • 정의
    • 'x'
    • "x"
    • repr(foo)
    • Char(decimal)
    • Char(hex)
  • 연산
    • "foo" < "bar"
    • "foo" > "bar"
    • "foo" == "bar"
    • "foo" != "bar"
    • isless("foo", "bar")
  • 판별
    • isspace(' ')
    • isletter('x')
    • isuppercase('X')
    • islowercase('x')
    • isdigit('1')
    • isxdigit('x')
    • isnumeric('1')
    • ispunct('!')
    • isascii("foo")
    • iscntrl('x')
    • isprint('x')
  • 일반
    • 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"
  • 변환
    • replace("foo bar", "o"=>"x")
    • reverse("foo bar")
    • lowercase("FOO BAR")
    • lowercasefirst("FOO BAR")
    • uppercase("foo bar")
    • uppercasefirst("foo bar")
    • titlecase("foo bar")
  • 추가/제거
    • 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")
  • 검색
    • 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)

정의

문법코드
'x'#홑 따옴표(',')로 문자(Char) 정의

julia> 'x'
'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)

julia> 'xx'
ERROR: ParseError:

julia> typeof('x')
Char
"x"#쌍 따옴표(")로 문자열(String) 정의

julia> "x"
"x"

julia> typeof("x")
String
repr(foo)#문자열 표현 반환

julia> repr(123)
"123"

julia> repr(zeros(3))
"[0.0, 0.0, 0.0]"
Char(decimal)

Char(hex)
#10진수/16진수를 유니코드 문자로 변환

julia> Char(0xAC00), Char(44032)
('가', '가')

연산

문법코드
"foo" < "bar"

"foo" > "bar"
#첫 글자의 유니코드 대소비교

julia> Int('W'), Int('Y')
(87, 89)

julia> "Wonyoung" < "Yujin"
true

julia> "Wonyoung" > "Yujin"
false
"foo" == "bar"

"foo" != "bar"
#두 문자열 비교

julia> "Wonyoung" == "wonyoung"
false

julia> "Wonyoung" == "Wonyoung"
true

julia> "Wonyoung" != "Wonyoung"
false

julia> "Wonyoung" != "wonyoung"
true
isless("foo", "bar")#"foo"가 유니코드로 "bar"보다 작은지 판별

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)

판별

문법코드
isspace(' ')#공백문자(공백, 개행, 탭) 판별

julia> isspace(' '), isspace('\n'), isspace('\t')
(true, true, true)

julia> isspace('x')
false
isletter('x')#유니코드 카테고리 문자(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')
#대소문자 판별

julia> isuppercase('j'), isuppercase('J')
(false, true)

julia> islowercase('j'), islowercase('J')
(true, false)
isdigit('1')

isxdigit('x')
#10진수/16진수 판별

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')#유니코드 카테고리 숫자(Nd, Nl, No) 판별

julia> isnumeric('1'), isnumeric('Ⅳ'), isnumeric('½'), isnumeric('௰')
(true, true, true, true)

julia> isnumeric('a'), isnumeric('가'), isnumeric('😊'), isnumeric('?')
(false, false, false, false)
ispunct('!')#구두점 판별

julia> ispunct('!'), ispunct('.'), ispunct('/'), ispunct('?')
(true, true, true, true)

julia> ispunct('1'), ispunct('x'), ispunct('가'), ispunct('😊')
(false, false, false, false)
isascii("foo")#아스키 코드 판별

julia> isascii("θ"), isascii("xθ"), isascii("123θ")
(false, false, false)

julia> isascii("x"), isascii("xyz"), isascii("123"), isascii("[")
(true, true, true, true)
iscntrl('x')#제어문자 판별

julia> iscntrl('x'), iscntrl('1'), iscntrl('\n'), iscntrl('\t')
(false, false, true, true)
isprint('x')#출력 가능 문자 판별

julia> isprint('x'), isprint('1'), isprint('\n'), isprint('\t')
(true, true, false, false)

일반

함수/문법코드
length("foo bar")#문자열 길이 반환

julia> length("Jang Wonyoung")
13
sizeof("foo bar")#문자열 바이트 크기 반환

julia> sizeof("xyz_. ?"), length("xyz_. ?")
(7, 7)

julia> sizeof("αβγ"), length("αβγ")
(6, 3)

julia> sizeof("😊😂"), length("😊😂")
(8, 2)
"foo bar"[1]

"foo bar"[1:3]
#문자열 인덱싱, 슬라이싱

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)
#문자열 첫/마지막 n개 문자 슬라이싱

julia> first("Jang Wonyoung", 3)
"Jan"

julia> last("Jang Wonyoung", 5)
"young"
"foo" * "bar"#문자열 연결, string("foo", "bar")와 같음음

julia> "Jang" * "Wonyoung"
"JangWonyoung"
string("foo", "bar")#문자열 연결, "foo" * "bar"와 같음

julia> string("Jang", "Wonyoung")
"JangWonyoung"
"foo"^n#문자열 연결 반복, repeat("foo", n)과 같음

julia> "Tung"^9
"TungTungTungTungTungTungTungTungTung"
repeat("foo", n)#문자열 연결 반복, "foo"^n과 같음

julia> repeat("Tung", 9)
"TungTungTungTungTungTungTungTungTung"
join(["foo1", "foo2"], "bar")#문자열 배열을 특정 구분자로 연결

julia> join(["Jang Wonyoung", "Karina", "Sullyoon"], " and ")
"Jang Wonyoung and Karina and Sullyoon"
split("foo bar", " ")#문자열을 특정 구분자로 분리

julia> split("Jang Wonyoung, Karina, Sullyoon", ", ")
3-element Vector{SubString{String}}:
"Jang Wonyoung"
"Karina"
"Sullyoon"
"foo $bar"#문자열 내삽interpolation

julia> name = "Wonyoung"
"Wonyoung"

julia> "Jang $name"
"Jang Wonyoung"

julia> "1 + 2 = 3" == "1 + 2 = $(1 + 2)"
true

변환

함수코드
replace("foo bar", "o"=>"x")#문자열에서 특정 문자 치환

julia> replace("izone", "zon"=>"v")
"ive"

julia> replace("izone", "z"=>"v", "o"=>"", "n"=>"")
"ive"
reverse("foo bar")#문자열 뒤집기

julia> reverse("Jang Wonyoung")
"gnuoynoW gnaJ"

lowercase("FOO BAR")#문자열 소문자화

julia> lowercase("JANG WONYOUNG")
"jang wonyoung"
lowercasefirst("FOO BAR")#첫 글자 소문자화

julia> lowercasefirst("JANG WONYOUNG")
"jANG WONYOUNG"
uppercase("foo bar")#문자열 대문자화

julia> uppercase("jang wonyoung")
"JANG WONYOUNG"
uppercasefirst("foo bar")#첫 글자 대문자화

julia> uppercasefirst("jang wonyoung")
"Jang wonyoung"
titlecase("foo bar")#각 어절 첫 글자자 대문자화

julia> titlecase("jang wonyoung")
"Jang Wonyoung"

추가/제거

함수코드
chop("foo bar", head=n, tail=m)#문자열 앞에서 n개, 뒤에서 m개 문자 제거

julia> chop("Jang Wonyoung", head=3, tail=5)
"g Won"
chopprefic("foo", "bar")

chopsuffix("foo", "bar")
#"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  ")#문자열 양 끝 공백 혹은 특정 문자 제거

julia> strip("     Julia Language   ")
"Julia Language"

julia> strip("     Julia Language___", [' ', '_'])
"Julia Language"
lstrip("  foo bar  ")

rstrip("  foo bar  ")
#문자열 왼쪽/오른쪽 끝 공백(특정문자) 제거

julia> lstrip("     Julia Language   ")
"Julia Language   "

julia> rstrip("     Julia Language_ _ ", [' ', '_'])
"     Julia Language"
lpad("foo", n)

rpad("foo", n, '_')
#문자열 길이 n으로 늘린 후 왼쪽/오른쪽 공백(특정문자) 추가

julia> lpad("xyz", 10)
"       xyz"

julia> rpad("xyz", 10, '_')
"xyz_______"
filter(function, "foo bar")#문자열에서 function을 만족하는 문자만 남김

julia> filter(isuppercase, "Jang Wonyoung")
"JW"

julia> filter(isspace, "Jang Wonyoung")
" "

julia> filter(!isspace, "Jang Wonyoung")
"JangWonyoung"

검색

함수코드
contains(haystack, needle)#haystack에 needle이 포함되어 있는지 확인

julia> contains("Jang Wonyoung", "y")
true

julia> contains("Jang Wonyoung", "x")
false
occursin(needle, haystack)#haystack에 needle이 포함되어 있는지 확인

julia> occursin("y", "Jang Wonyoung")
true

julia> occursin("x", "Jang Wonyoung")
false
startswith(haystack, needle)#haystack이 needle로 시작하는지 확인

julia> startswith("hello", "he")
true
endswith(haystack, needle)#haystack이 needle로 끝나는지 확인

julia> endswith("hello", "lo")
true
findfirst("foo", "bar")#문자열에서 특정 문자 위치 찾기 (왼쪽 기준)

julia> findfirst('g', "Jang wonyoung")
4

julia> findfirst("ng", "Jang wonyoung")
3:4
findlast("foo", "bar")#문자열에서 특정 문자 위치 찾기 (오른쪽 기준)

julia> findlast('g', "Jang wonyoung")
13

julia> findlast("ng", "Jang wonyoung")
12:13
findnext("foo", "bar", n)#문자열에서 특정 문자 위치 찾기 (n번째 이후로만 탐색)

julia> findnext('g', "Jang wonyoung", 2)
4

julia> findnext('g', "Jang wonyoung", 6)
13
findprev("foo", "bar", n)#문자열에서 특정 문자 위치 찾기 (n번째 이전으로만 탐색)

julia> findprev('o', "Jang wonyoung", 13)
10

julia> findprev('o', "Jang wonyoung", 9)
7
findall(function, haystack)#haystack에서 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