3,382
个编辑
MCBBS Wiki欢迎您共同参与编辑!在参与编辑之前请先阅读Wiki方针。
如果在编辑的过程中遇到了什么问题,可以去讨论板提问。
为了您能够无阻碍地参与编辑 未验证/绑定过邮箱的用户,请尽快绑定/验证。
MCBBS Wiki GitHub群组已上线!
您可以在回声洞中发表吐槽!
服务器状态监控。点击进入
本站由MCBBS用户自行搭建,与MCBBS及东银河系漫游指南(北京)科技有限公司没有从属关系。点此了解 MCBBS Wiki 不是什么>>
Sheep-realms(留言 | 贡献) (创建页面,内容为“--[[ 引自维基百科(enwp:Module:String,oldid=552254999) This module is intended to provide access to basic string functions. Most of the functions provided here can be invoked with named parameters, unnamed parameters, or a mixture. If named parameters are used, Mediawiki will automatically remove any leading or trailing whitespace from the parameter. Depending on the intended use, it may be advantageous to either preserve or remove such whi…”) |
Salt lovely(留言 | 贡献) (更新“模块:String”并翻译部分内容 // 维基盐编辑器) |
||
-- 此模块引用自维基百科同名页面,引用行为依据 CC BY-SA 3.0 条款
-- 部分内容因为本地化或其他修改,可能与原模块有出入
--[[
该模块提供了基本的字符串函数。
这里的大多数函数都能以命名参数、匿名参数或混合前两种的形式调用。
若使用命名参数,Mediawiki 将自动删除参数前后的空格符号。
是否应该删除或保留前后空格,取决于具体用途。
全局配置
ignore_errors: 如果设为 'true' 或 1,任何出错都将返回空字符串而非报错信息。
error_category: 如果发生异常,报错信息会自动添加一个分类到页面上,默认分类是
[Category:字符串模块报告的错误]。
no_category:
]]
len
返回目标字符串的长度。
用法:
{{#invoke:String|len|
或
{{#invoke:String|len|s=
参数:
s:
]]
function str.len( frame )
sub
根据指定索引,从目标字符串中获取子字符串
用法
{{#invoke:String|sub|目标字符串|开始位置|结束位置}}
或
{{#invoke:String|sub|s=目标字符串|i=开始位置|j=结束位置}}
参数:
s:
i:
j:
字符串
如果 i 或 j 传入了负数,将视为从字符串末尾开始计数的索引。
比如 -1 会视为字符串的最后一位。
如果
]]
function str.sub( frame )
local len = mw.ustring.len( s );
-- 转换负数并检查区间
if i < 0 then
i = len + i + 1;
if i > len or j > len or i < 1 or j < 1 then
return str._error( '
end
if j < i then
return str._error( '
end
match
根据指定模式,从目标字符串中获取匹配的子字符串
用法:
{{#invoke:String|match|
或
{{#invoke:String|pos|s=
|match=match_number|plain=plain_flag|nomatch=
参数:
s: 目标字符串,以它查找匹配
pattern: 在目标字符串中查找用的,指定模式或字符串
start: The index within the source string to start the search. The first
character of the string has index 1.
match: In some cases it may be possible to make multiple matches on a single
string. This specifies which match to return, where the first match is
match= 1. If a negative number is specified then a match is returned
counting from the last match. Hence match = -1 is the same as requesting
the last match.
plain: 指示 pattern 参数是否视为"字符串"的标记,默认为否(即默认视为"模式")。
nomatch: 如果找不到匹配项,输出 "nomatch" 的值而非报错。
若使用命名参数(第二种调用方法),Mediawiki 将自动删除参数前后的空格符号。
If the match_number or start_index are out of range for the string being queried, then
an empty string will be returned on any failure.
关于构造 Lua 匹配模式的更多信息,一种[正则表达式],参见:
* http://www.lua.org/manual/5.1/manual.html#5.4.1
if s == '' then
return str._error( '
end
if pattern == '' then
return str._error( '
end
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then
return str._error( '
end
if match_index == 0 then
return str._error( '
end
if plain_flag then
if result == nil then
if nomatch == nil then
return str._error( '
else
return nomatch;
pos
返回目标字符串在索引位置的那个字符。
用法:
{{#invoke:String|pos|
OR
{{#invoke:String|pos|target=
参数:
target:
pos: 需要返回的字符之索引位置
If invoked using named parameters, Mediawiki will automatically remove any leading or
other cases one may want to preserve the whitespace.
字符串第一个字符的索引为 1。
If one requests a negative value, this function will select a character by counting backwards
if pos == 0 or math.abs(pos) > mw.ustring.len( target_str ) then
return str._error( '
end
string.
用法:
{{#invoke:String|find|source_str|target_string|start_index|plain_flag}}
OR
{{#invoke:String|find|source=source_str|target=target_str|start=start_index|plain=plain_flag}}
参数:
source: The string to search
target: The string or pattern to find within source
start: The index within the source string to start the search,
plain: 指示 pattern 参数是否视为"字符串"而非 Lua 风格的正则表达式的 Boolean 标记, 默认为 true。
If invoked using named parameters, Mediawiki will automatically remove any leading or
string.
用法:
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
OR
count=replacement_count|plain=plain_flag}}
参数:
source: The string to search
pattern: The string or pattern to find within source
replace: The replacement text
count: The number of occurences to replace,
plain: 指示 pattern 参数是否视为"字符串"而非 Lua 风格的正则表达式的 Boolean 标记, 默认为 true。
]]
function str.replace( frame )
return string.rep( frame.args[1] or '', repetitions )
end
function str.split(inputstr, sep, no_pattern, ignore_null)
--#invoke 支
if type(inputstr) == type({table}) then
if not getArgs then getArgs = require('Module:Arguments').getArgs end
length = mw.ustring.len(input_str)
--split函
if no_pattern_flag then
separ = mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(separ,
join
合并所有非空参数;第一个参数是分隔符。
用法:
{{#invoke:String|join|分隔符|文字1|文字2|文字3}}
]]
function str.join(frame)
--[[
处理报错信息的辅助函数。
]]
function str._error( error_str )
local frame = mw.getCurrentFrame();
local error_category = frame.args.error_category or '
local ignore_errors = frame.args.ignore_errors or false;
local no_category = frame.args.no_category or false;
end
local error_str = '<strong class="error">
if error_category ~= '' and not str._getBoolean( no_category ) then
error_str = '[[Category:' .. error_category .. ']]' .. error_str;
--[[
]]
function str._getBoolean( boolean_str )
boolean_value = boolean_str;
else
error( '
end
return boolean_value
--[[
转换正则表达式字符,使之能作为普通字符串解析的辅助函数。
]]
function str._escapePattern( pattern_str )
|