Mòdul:StringFunc: diferència entre les revisions
Contingut suprimit Contingut afegit
mCap resum de modificació |
arreglo funció split, només per text pla |
||
Línia 37:
Split
This function Splits a string based on a
Usage:
{{#invoke:StringFunc|split|source_string|
Parameters:
source: The string to return a subset of
count: The nth substring based on the pattern to return
]]
function p.split( frame )
local new_args = p._getParameters( frame.args, {'source', 'pattern', 'count
local source_str = new_args['source'] or '';
local pattern = new_args['pattern'] or '';
local pattern_len = mw.ustring.len(pattern);
if source_str == '' or pattern == '' then
return source_str;
end
local l_plain = p._getBoolean( new_args['plain'] or true );
▲ pattern = p._escapePattern( pattern );
pattern = "[" .. pattern .. "]"
local ret_count = tonumber( new_args['count'] ) or 1;
local start = 1;
local iter = mw.ustring.find(source_str, pattern, start, plain) - 1;
if iter == nil then
return source_str;
Linha 71 ⟶ 68:
end
for i=2, ret_count do
start = iter+pattern_len + 1;
iter = mw.ustring.find(source_str, pattern, start, plain) - 1;
if iter == nil then
break
|