Module:Substring?

From Wikinews, the free news source you can write!
Jump to navigation Jump to search
[edit] Documentation

Function[edit]

A single function check is exported, for use by {{substring?}}. Function check returns blank unless the second parameter occurs within the first, in which case it returns the 1-indexed position of the second parameter within the first.

The substring-check by this module is not meant to be part of a larger suite of string-manipulation tools. Use Module:Wikilisp for general string manipulation. Sometimes a page contains very many operations that can almost be handled using magic words, but fall short because they need to distinguish between cases in a way that this module can provide. In such a situation, this module should afford a huge savings over Module:Wikilisp, even if the more general tool is used for some rare cases selected using this tool.

local export = {}

function export.check( frame )
	local x = frame.args[1]
	if (x == nil) then return "" end
	local y = frame.args[2]
	if (y == nil) then return "" end
	local z = mw.ustring.find( x, y, 1, true )
	if (z ~= nil) then return z end
	return ""
end

return export