Homepage
Games
News
News βΎ
All
Transfers
Drama
Business
Sponsors
Mergers
Investors
Hires
Companies
More
People
Tournaments
Brands
Brands βΎ
Companies
Organizations
Sponsors
Other
Locations
Search
Editing
Module:StringUtils
From TwogPedia
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
local String = {} function String.startsWith(str, start) return str:sub(1, #start) == start --str:find('^' .. start) ~= nil end function String.endsWith(str, ending) return ending == '' or str:sub(-#ending) == ending end function String.split(inputstr, sep) if inputstr ~= nil then if sep == nil then sep = '%s' end inputstr = inputstr:gsub(sep, '&') local t = {} local i = 1 for str in string.gmatch(inputstr, '([^&]+)') do t[i] = str i = i + 1 end return t else return {''} end end -- need to escape .()[]+-% with % for 'match' function String.contains(str, match) return string.find(str, match) ~= nil end function String.trim(str) return (str:gsub('^%s*(.-)%s*$', '%1')) end function String.nilIfEmpty(str) return str ~= '' and str or nil end function String.isEmpty(str) return str == nil or str == '' end function String.isNotEmpty(str) return str ~= nil and str ~= '' end -- index counts up from 0 function String.explode(str, delimiter, index) return String.split(str, delimiter)[index + 1] or '' end --transforms a wiki code list ---->> * text ---->> * text --into a html list (with ul/li tags) function String.convertWikiListToHtmlList(str, delimiter) if String.isEmpty(str) then return '' end if String.isEmpty(delimiter) then delimiter = '*' end local strArray = mw.text.split(str, delimiter) local list = mw.html.create('ul') for _, item in ipairs(strArray) do if not String.isEmpty(item) then list:tag('li'):wikitext(item) end end return tostring(list) end -- Create a string with string interpolation -- -- String.interpolation('I\'m ${age} years old', {age = 40}) -- Returns `I'm 40 years old` -- -- Inspiration: http://lua-users.org/wiki/StringInterpolation function String.interpolate(s, tbl) return ( s:gsub('($%b{})', function(w) return tbl[w:sub(3, -2)] or w end ) ) end return String
Summary:
Please note that all contributions to TwogPedia may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
TwogPedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Preview page with this template
Template used on this page:
Module:StringUtils/doc
(
edit
)
No categories
Edit