codecamp

Elixir 字符串

Elixir中的字符串用双引号包围,并且以UTF-8格式编码:

iex> "hellö"
"hello"
注意:Windows系统的终端有可能默认不实用UTF-8。你可以在进入IEx之前输入来改变当前会话的编码。​chcp 65001

Elixir也支持格式化字符串:

iex> "hellö #{:world}"
"hellö world"

字符串可以包含换行。你可以用转义字符来显示它们:

iex> "hello
...> world"
"hello\nworld"
iex> "hello\nworld"
"hello\nworld"

你可以使用模块中的函数来打印字符串:​IO​IO.put/1

iex> IO.puts "hello\nworld"
hello
world
:ok

注意'IO.puts/1'函数在打印完之后返回了原子作为结果。​:ok

Elixir中的字符串在内部以二进制来表示,并且按字节排序:

iex> is_binary("hellö")
true

我们还可以获得字符串的字节数:

iex> byte_size("hellö")
6

注意这个字符串的字节数是6,即使它只有5个字符。这是因为字符“ö”在UTF-8编码中占用了两个字节。我们可以使用函数来获取基于字符数的字符串长度:​String.length/1

iex> String.length("hellö")
5

在字符串模块中包含了一系列基于Unicode标准对字符串进行处理的函数:

iex> String.upcase("hellö")
"HELLÖ"


Elixir 原子
Elixir 匿名函数
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Elixir 基本操作符

Elixir 二进制,字符串和字符列表

Elixir 类型规格与行为

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }