2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでelixirAdvent Calendar 2022

Day 9

paiza.ioでelixir その34

Last updated at Posted at 2022-11-02

概要

paiza.ioでelixirやってみた。
String.codepoints使ってみた。

参考にしたページ

サンプルコード

defmodule StringHelper do
	@moduledoc """
	文字列を扱う汎用的なヘルパー関数群。
	"""
	@doc """
	与えられた文字列に含まれるひらがなをカタカナに変換して返す。
	"""
	def hiragana2katakana(source) do
		source
		|> String.codepoints()
		|> Enum.map(&do_convert_h2k(&1))
		|> Enum.join()
	end
	defp do_convert_h2k(cp) when cp < "\u3041" or cp > "\u3096", do: cp
	defp do_convert_h2k(cp) do
		<<n::utf8>> = cp
		m = n + 0x60
		<<m::utf8>>
	end
	@doc """
	与えられた文字列に含まれるカタカナをひらがなに変換して返す。
	"""
	def katakana2hiragana(source) do
		source
		|> String.codepoints()
		|> Enum.map(&do_convert_k2h(&1))
		|> Enum.join()
	end
	defp do_convert_k2h(cp) when cp < "\u30A1" or cp > "\u30F6", do: cp
	defp do_convert_k2h(cp) do
		<<n::utf8>> = cp
		m = n - 0x60
		<<m::utf8>>
	end
end


StringHelper.hiragana2katakana("かっとやさい")
|> IO.puts

StringHelper.katakana2hiragana("ダブルクォート")
|> IO.puts


実行結果

カットヤサイ
だぶるくぉーと

成果物

以上。

2
0
0

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?