字符类型 #

一、字符基础 #

1.1 创建字符 #

使用单引号创建字符:

julia
c1 = 'A'
c2 = '中'
c3 = '😀'

1.2 字符类型 #

julia
typeof('A')
typeof('中')

1.3 字符与字符串 #

字符和字符串是不同的类型:

julia
'A' isa Char
"A" isa String
'A' == "A"

二、字符编码 #

2.1 Unicode码点 #

每个字符对应一个Unicode码点:

julia
Int('A')
Int('中')
Int('😀')
codepoint('A')

2.2 从码点创建字符 #

julia
Char(65)
Char(20013)
Char(0x1F600)
Char('\u0041')
Char('\U0001F600')

2.3 转义序列 #

julia
'\n'
'\t'
'\\'
'\''
'\u0041'

2.4 字符大小 #

julia
sizeof('A')
sizeof('中')
sizeof('😀')
ncodeunits('A')
ncodeunits('中')
ncodeunits('😀')

三、字符分类 #

3.1 字母和数字 #

julia
isalpha('A')
isalpha('中')
isalpha('5')

isdigit('5')
isdigit('A')
isdigit('五')

isalnum('A')
isalnum('5')
isalnum('!')

3.2 大小写 #

julia
islower('a')
islower('A')
isupper('A')
isupper('a')

3.3 空白字符 #

julia
isspace(' ')
isspace('\t')
isspace('\n')
isspace('A')

3.4 标点符号 #

julia
ispunct('!')
ispunct(',')
ispunct('A')

3.5 控制字符 #

julia
iscntrl('\n')
iscntrl('\t')
iscntrl('A')

3.6 ASCII字符 #

julia
isascii('A')
isascii('中')

四、字符转换 #

4.1 大小写转换 #

julia
uppercase('a')
lowercase('A')
titlecase('a')

4.2 字符与数字 #

julia
Int('5') - Int('0')
digit('5')
digit('5', 10)

4.3 字符与字符串 #

julia
string('A')
string('中')
String(['H', 'e', 'l', 'l', 'o'])

五、字符比较 #

5.1 相等比较 #

julia
'A' == 'A'
'A' == 'a'
'A' === 'A'

5.2 大小比较 #

julia
'A' < 'B'
'a' < 'A'
'中' < '日'

5.3 字符范围 #

julia
'A':'Z'
'a':'z'
'0':'9'
collect('A':'Z')

六、字符迭代 #

6.1 字符串中的字符 #

julia
s = "Hello"
for c in s
    println(c)
end

6.2 字符数组 #

julia
arr = ['H', 'e', 'l', 'l', 'o']
join(arr)
String(arr)

6.3 字符生成 #

julia
collect('a':'z')
[Char(i) for i in 65:90]

七、特殊字符 #

7.1 控制字符 #

julia
'\0'
'\n'
'\r'
'\t'
'\v'
'\b'
'\f'

7.2 Unicode类别 #

julia
isletter('A')
isletter('中')
isnumeric('5')
isnumeric('五')

7.3 数学符号 #

julia
'π'
'∑'
'√'
'∞'

八、实践练习 #

8.1 练习1:字符统计 #

julia
function char_stats(s::String)
    letters = 0
    digits = 0
    spaces = 0
    others = 0
    
    for c in s
        if isalpha(c)
            letters += 1
        elseif isdigit(c)
            digits += 1
        elseif isspace(c)
            spaces += 1
        else
            others += 1
        end
    end
    
    return (letters=letters, digits=digits, spaces=spaces, others=others)
end

char_stats("Hello 123 World!")

8.2 练习2:凯撒密码 #

julia
function caesar_cipher(text::String, shift::Int)
    result = Char[]
    for c in text
        if isalpha(c)
            base = isuppercase(c) ? 'A' : 'a'
            shifted = (c - base + shift) % 26 + base
            push!(result, Char(shifted))
        else
            push!(result, c)
        end
    end
    return String(result)
end

caesar_cipher("Hello World", 3)
caesar_cipher("Khoor Zruog", -3)

8.3 练习3:字符频率 #

julia
function char_frequency(s::String)
    freq = Dict{Char, Int}()
    for c in s
        freq[c] = get(freq, c, 0) + 1
    end
    return freq
end

char_frequency("Hello World")

九、总结 #

本章我们学习了:

  1. 字符创建:使用单引号创建字符
  2. 字符编码:Unicode码点和字符转换
  3. 字符分类:字母、数字、空白、标点等
  4. 字符转换:大小写转换和类型转换
  5. 字符比较:相等和大小比较
  6. 字符迭代:遍历字符串中的字符

接下来让我们学习Julia的布尔类型!

最后更新:2026-03-27