字符串连接

iex(31)> "Hello" <> " " <> "World"
"Hello World"

参考: Kernel.<>/2

字符串插值

iex(29)> "Hello #{21 + 21}"             
"Hello 42"

原子转字符串

iex(27)> Atom.to_string(:hello)          
"hello"

连接字符串列表

iex(24)> "1,2,3" = Enum.join(["1","2","3"], ",")
"1,2,3"

删除前后空白

iex(23)> String.strip("    a     ")
"a"

替换

iex(22)> String.replace("1.2.3", ".", "") 
"123"

切分

iex(21)> String.split("1,2,3" , ",")
["1", "2", "3"]

解析为浮点数

iex(20)> Float.parse("3.14")
{3.14, ""}

解析为整数

iex(19)> Integer.parse("4")
{4, ""}

反转

iex(18)> String.reverse("hello")
"olleh"

developerworks
1.7k 声望266 粉丝