Module:String
From Wiki
The String script wraps select functions of the Ustring library. Usage:
{{#invoke:String|FUNCTION|ARG1|ARG2…}}
Character positions within a string are indexed from 1, not zero. The following call to function sub would, for instance, return the substring "ham".
{{#invoke:String|sub|hamburger|1|3}}
The functions listed below are wrapped and made available. The links are to the String version documentation. These in turn link to the less informative Ustring versions, which are always the ones actually wrapped here.
- find - Differs from the library version in three ways: 1) defaults the 'plain' argument to true; 2) returns the index at which the pattern is found, otherwise zero; and 3) always returns zero when called with an empty string or pattern.
- gsub
- len
- sub
The following additional functions are declared. See the code below for usage instructions.
- charAt
-- Cf. https://en.wikipedia.org/wiki/Module:String
--
local p = {}
------------------------------------------------------------------------------------------
-- charAt - return the character at index C
--
-- charAt(S,C)
--
function p.charAt( frame )
local c = tonumber( frame.args[2] )
return mw.ustring.sub( frame.args[1], c, c )
end
------------------------------------------------------------------------------------------
-- find
--
-- Differs from the library version in three ways: 1) defaults the 'plain' argument to true;
-- 2) returns the index at which the pattern is found, otherwise zero; and 3) always returns
-- zero when called with an empty string or pattern.
--
-- Modified from https://en.wikipedia.org/wiki/Module:String
--
function p.find( frame )
local s = frame.args[1]
local pattern = frame.args[2]
if s == '' or pattern == '' then
return 0
end
local plain = frame.args[4]
if plain == nil then
plain = true
else
plain = toBoolean( plain )
end
local start = mw.ustring.find( s, pattern, tonumber(frame.args[3]), plain )
if start == nil then
start = 0
end
return start
end
------------------------------------------------------------------------------------------
function p.gsub( frame )
local result = mw.ustring.gsub( frame.args[1], frame.args[2], frame.args[3], tonumber(frame.args[4]) ) -- ignoring 2nd return value (count)
return result
end
------------------------------------------------------------------------------------------
function p.len( frame )
return mw.ustring.len( frame.args[1] )
end
------------------------------------------------------------------------------------------
function p.sub( frame )
return mw.ustring.sub( frame.args[1], tonumber(frame.args[2]), tonumber(frame.args[3]) )
end
------------------------------------------------------------------------------------------
-- toBoolean - translate a MediaWiki string to a Lua boolean
--
-- Modified from https://en.wikipedia.org/wiki/Module:String
--
function toBoolean( s )
local b;
if s == nil then
b = false;
else
local sType = type( s )
if sType == 'string' then
s = s:lower();
b = s ~= 'false' and s ~= 'no' and s ~= '0' and s ~= ''
elseif sType == 'boolean' then
b = s;
else
error( 'Not a boolean value' )
end
end
return b
end
return p