Help us understand the problem. What is going on with this article?

Elixir文字列まとめ

More than 3 years have passed since last update.

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした