LoginSignup
13
12

More than 5 years have passed since last update.

Elixir文字列まとめ

Posted at

Elixirの文字列まとめ

A String in Elixir is a UTF-8 encoded binary.

普通に表示

hello.exs
IO.puts "Hello, World!"

文字列結合

文字列同士の結合

string1.exs
# "YamadaTaro"
IO.puts "Yamada " <> "Taro"

指定文字列での結合

string2.exs
res1 = Enum.join(["abc", "efg", "hij"], "-")
res2 = Enum.join(["abc", "efg", "hij"], ",")
# "abc-efg-hij"
IO.puts res1
# "abc,efg,hij"
IO.puts res2

インデックスアクセス

string3.exs
# "i"
IO.puts String.at "Elixir", 2

最初の文字、最後の文字にアクセス

string4.exs
# E
IO.puts String.first "Elixir"
# r
IO.puts String.last "Elixir"

部分文字列

string5.exs
# Eli
IO.puts String.slice "Elixir", 0, 3

文字列への式展開

string6.exs
a = "Elixir"
# "Hello Elixir"
IO.puts "Hello #{a}"

文字列の長さ

string7.exs
# 6
IO.puts String.length "Elixir"

指定文字での分割

string8.exs
hello = String.split "Hello,Elixir!", ","
# Hello
IO.puts hd hello
# Elixir!
IO.puts tl hello

バイト数確認

string9.exs
# 6
IO.puts byte_size "Elixir"

大文字・小文字変換

string10.exs
# "ELIXIR"
IO.puts String.upcase "elixir"
# elixir
IO.puts String.downcase "ELIXIR"

置換

string11.exs
# hello Elixir!
IO.puts String.replace "hello elixir!", "elixir", "Elixir"

参考
http://elixir-lang.org/docs/stable/elixir/String.html

13
12
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12