LoginSignup
4

More than 5 years have passed since last update.

Lua言語の便利スニペットあれこれ

Posted at

Luaは記述しやすく、動作も早くて気に入ってきました。そこで、便利スニペットを色々用意することにしました。
(逐次追加していく予定です)

文字列を特定の文字で分割し配列を返す

function split(str, ts)
  -- 引数がないときは空tableを返す
  if ts == nil then return {} end

  local t = {} ; 
  i=1
  for s in string.gmatch(str, "([^"..ts.."]+)") do
    t[i] = s
    i = i + 1
  end

  return t
end

使い方

url = "http://test.com/test_content/index"
url_splited = split(url, "/")

--[[
  url_splited => {"http:", "test.com", "test_content", "index"}
]]---

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
4