Guest User

strplus

a guest
Jun 27th, 2014
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. function toCharTable(str)
  2.   if type(str) ~= "string" then
  3.     error("String needed, got "..type(str))
  4.     return
  5.   end
  6.   local t={}
  7.   for i=1,#str do
  8.     t[i] = str:sub(i,i)
  9.   end
  10.   return t
  11. end
  12. function fromCharTable(table)
  13.   if type(table) ~= "table" then
  14.     error("Char table needed, got "..type(table))
  15.     return
  16.   end
  17.   local str = ""
  18.   for k,v in pairs(table) do
  19.     str = str..v
  20.   end
  21.   return str
  22. end
  23. function separate(str,separator)
  24.   if type(str) ~= "string" then
  25.     error("String needed, got "..type(str))
  26.   elseif type(separator) ~= "string" then
  27.     error("Separator:String needed, got "..type(separator))
  28.   end
  29.   local t={}
  30.   for i=1,#str do
  31.     t[i] = str:sub(i,i)
  32.   end
  33.   local t2 = {}
  34.   local cstr = ""
  35.   local cindex = 0
  36.   for i=1,#t do
  37.     if t[i] == separator then
  38.       cindex = cindex+1
  39.       t2[cindex] = cstr
  40.       cstr = ""
  41.     else
  42.       cstr = cstr..t[i]
  43.     end
  44.   end
  45.   if cstr ~= "" then
  46.     t2[cindex+1] = cstr
  47.   end
  48.   return t2
  49.    
  50. end
  51. function replace(str,toreplace,replacewith)
  52.   if type(str) ~= "string" or type(toreplace) ~= "string" or type(replacewith) ~= "string" then
  53.     error("Cannot replace a "..type(str).." if thing to replace is "..type(toreplace).." and the thing with replace is "..type(replacewith))
  54.   end
  55.   t = {}
  56.   for i=1,#str do
  57.     t[i] = str:sub(i,i)
  58.   end
  59.   local retstr = ""
  60.   for i=1,#t do
  61.     if t[i] == toreplace then
  62.       retstr = retstr .. replacewith
  63.     else
  64.       retstr = retstr .. t[i]
  65.     end
  66.   end
  67.   return retstr
  68. end
  69. function unseparate(table,separator)
  70.   if type(table) ~= "table" then error("Table needed, got "..type(table)) end
  71.   if type(separator) ~= "string" then error("String needed as separator, got "..type(separator)) end
  72.   local string = ""
  73.   for i=1,#table do
  74.     string = string..table[i]..separator
  75.   end
  76.   return string
  77. end
  78. function indexChar(str,index)
  79.   if type(index) ~= "number" then error("As index, number needed, got "..type(index)) end
  80.   if type(str) ~= "string" then error("String needed, got "..type(str)) end
  81.   local char = str:sub(index,index)
  82.   return char
  83. end
  84. function credits()
  85.   local credits = "Created by Konlab, inspired by tomass1996's StrUtils API, but don't used any part of that API, I (Konlab) don't saw the StrUtils' code before creating this API, you can edit, redistribute, copy etc. if you give me credits"
  86.   return credits  
  87. end
Advertisement
Add Comment
Please, Sign In to add comment