3,389
个编辑
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”并翻译部分内容 // 维基盐编辑器) |
||
| 第1行: | 第1行: | ||
-- 此模块引用自维基百科同名页面,引用行为依据 CC BY-SA 3.0 条款 | |||
-- 部分内容因为本地化或其他修改,可能与原模块有出入 | |||
--[[ | --[[ | ||
该模块提供了基本的字符串函数。 | |||
这里的大多数函数都能以命名参数、匿名参数或混合前两种的形式调用。 | |||
若使用命名参数,Mediawiki 将自动删除参数前后的空格符号。 | |||
是否应该删除或保留前后空格,取决于具体用途。 | |||
全局配置 | |||
ignore_errors: 如果设为 'true' 或 1,任何出错都将返回空字符串而非报错信息。 | |||
ignore_errors: | |||
error_category: 如果发生异常,报错信息会自动添加一个分类到页面上,默认分类是 | |||
[Category:字符串模块报告的错误]。 | |||
no_category: 如果设为 'true' 或 1,报错时不会添加分类。 | |||
你可以在 Module:String/tests 找到单元测试。 | |||
]] | ]] | ||
| 第47行: | 第25行: | ||
len | len | ||
返回目标字符串的长度。 | |||
用法: | |||
{{#invoke:String|len|目标字符串|}} | |||
或 | |||
{{#invoke:String|len|s=目标字符串}} | |||
参数: | |||
s: 要计算长度的字符串 | |||
若使用命名参数(第二种调用方法),Mediawiki 将自动删除目标字符串前后的空格符号。 | |||
]] | ]] | ||
function str.len( frame ) | function str.len( frame ) | ||
| 第84行: | 第46行: | ||
sub | sub | ||
根据指定索引,从目标字符串中获取子字符串 | |||
用法: | |||
{{#invoke:String|sub| | {{#invoke:String|sub|目标字符串|开始位置|结束位置}} | ||
或 | |||
{{#invoke:String|sub|s= | {{#invoke:String|sub|s=目标字符串|i=开始位置|j=结束位置}} | ||
参数: | |||
s: 目标字符串,子字符串来自于它。 | |||
i: 子字符串的开始位置,默认为 1。 | |||
j: 子字符串的结束位置,默认为最后一位。 | |||
字符串第一个字符的索引为 1。 | |||
如果 i 或 j 传入了负数,将视为从字符串末尾开始计数的索引。 | |||
比如 -1 会视为字符串的最后一位。 | |||
如果索引位置超出字符串长度,将会报错。 | |||
]] | ]] | ||
function str.sub( frame ) | function str.sub( frame ) | ||
| 第134行: | 第72行: | ||
local len = mw.ustring.len( s ); | local len = mw.ustring.len( s ); | ||
-- | -- 转换负数并检查区间 | ||
if i < 0 then | if i < 0 then | ||
i = len + i + 1; | i = len + i + 1; | ||
| 第143行: | 第81行: | ||
if i > len or j > len or i < 1 or j < 1 then | if i > len or j > len or i < 1 or j < 1 then | ||
return str._error( ' | return str._error( '截取字符串索引脱离区间' ); | ||
end | end | ||
if j < i then | if j < i then | ||
return str._error( ' | return str._error( '截取字符串指示脱离顺序' ); | ||
end | end | ||
| 第165行: | 第103行: | ||
match | match | ||
根据指定模式,从目标字符串中获取匹配的子字符串 | |||
用法: | |||
{{#invoke:String|match| | {{#invoke:String|match|目标字符串|pattern_string|start_index|match_number|plain_flag|无匹配输出}} | ||
或 | |||
{{#invoke:String|pos|s= | {{#invoke:String|pos|s=目标字符串|pattern=pattern_string|start=start_index | ||
|match=match_number|plain=plain_flag|nomatch= | |match=match_number|plain=plain_flag|nomatch=无匹配输出}} | ||
参数: | |||
s: | s: 目标字符串,以它查找匹配 | ||
pattern: | pattern: 在目标字符串中查找用的,指定模式或字符串 | ||
start: The index within the source string to start the search. The first | start: The index within the source string to start the search. The first | ||
character of the string has index 1. | character of the string has index 1. 默认为 1。 | ||
match: In some cases it may be possible to make multiple matches on a single | 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 | 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 | 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 | counting from the last match. Hence match = -1 is the same as requesting | ||
the last match. | the last match. 默认为 1。 | ||
plain: | plain: 指示 pattern 参数是否视为"字符串"的标记,默认为否(即默认视为"模式")。 | ||
nomatch: 如果找不到匹配项,输出 "nomatch" 的值而非报错。 | |||
nomatch: | |||
若使用命名参数(第二种调用方法),Mediawiki 将自动删除参数前后的空格符号。 | |||
In some circumstances this is desirable, in other cases one may want to preserve the whitespace. | |||
other cases one may want to preserve the whitespace. | |||
If the match_number or start_index are out of range for the string being queried, then | If the match_number or start_index are out of range for the string being queried, then | ||
| 第197行: | 第132行: | ||
an empty string will be returned on any failure. | an empty string will be returned on any failure. | ||
关于构造 Lua 匹配模式的更多信息,一种[正则表达式],参见: | |||
* http://www.lua.org/manual/5.1/manual.html#5.4.1 | * http://www.lua.org/manual/5.1/manual.html#5.4.1 | ||
| 第214行: | 第149行: | ||
if s == '' then | if s == '' then | ||
return str._error( ' | return str._error( '目标字符串是空的' ); | ||
end | end | ||
if pattern == '' then | if pattern == '' then | ||
return str._error( ' | return str._error( '模式字符串是空的' ); | ||
end | end | ||
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then | if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then | ||
return str._error( ' | return str._error( '要求的起始点脱离区间' ); | ||
end | end | ||
if match_index == 0 then | if match_index == 0 then | ||
return str._error( ' | return str._error( '匹配索引脱离区间' ); | ||
end | end | ||
if plain_flag then | if plain_flag then | ||
| 第263行: | 第198行: | ||
if result == nil then | if result == nil then | ||
if nomatch == nil then | if nomatch == nil then | ||
return str._error( ' | return str._error( '找不到匹配' ); | ||
else | else | ||
return nomatch; | return nomatch; | ||
| 第275行: | 第210行: | ||
pos | pos | ||
返回目标字符串在索引位置的那个字符。 | |||
用法: | |||
{{#invoke:String|pos| | {{#invoke:String|pos|目标字符串|索引值}} | ||
OR | OR | ||
{{#invoke:String|pos|target= | {{#invoke:String|pos|target=目标字符串|pos=索引值}} | ||
参数: | |||
target: | target: 目标字符串,从它获取字符 | ||
pos: | pos: 需要返回的字符之索引位置 | ||
If invoked using named parameters, Mediawiki will automatically remove any leading or | If invoked using named parameters, Mediawiki will automatically remove any leading or | ||
| 第290行: | 第225行: | ||
other cases one may want to preserve the whitespace. | 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 one requests a negative value, this function will select a character by counting backwards | ||
| 第303行: | 第238行: | ||
if pos == 0 or math.abs(pos) > mw.ustring.len( target_str ) then | if pos == 0 or math.abs(pos) > mw.ustring.len( target_str ) then | ||
return str._error( ' | return str._error( '字符串索引脱离区间' ); | ||
end | end | ||
| 第346行: | 第281行: | ||
string. | string. | ||
用法: | |||
{{#invoke:String|find|source_str|target_string|start_index|plain_flag}} | {{#invoke:String|find|source_str|target_string|start_index|plain_flag}} | ||
OR | OR | ||
{{#invoke:String|find|source=source_str|target=target_str|start=start_index|plain=plain_flag}} | {{#invoke:String|find|source=source_str|target=target_str|start=start_index|plain=plain_flag}} | ||
参数: | |||
source: The string to search | source: The string to search | ||
target: The string or pattern to find within source | target: The string or pattern to find within source | ||
start: The index within the source string to start the search, | start: The index within the source string to start the search, 默认为 1 | ||
plain: Boolean | plain: 指示 pattern 参数是否视为"字符串"而非 Lua 风格的正则表达式的 Boolean 标记, 默认为 true。 | ||
If invoked using named parameters, Mediawiki will automatically remove any leading or | If invoked using named parameters, Mediawiki will automatically remove any leading or | ||
| 第396行: | 第330行: | ||
string. | string. | ||
用法: | |||
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}} | {{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}} | ||
OR | OR | ||
| 第402行: | 第336行: | ||
count=replacement_count|plain=plain_flag}} | count=replacement_count|plain=plain_flag}} | ||
参数: | |||
source: The string to search | source: The string to search | ||
pattern: The string or pattern to find within source | pattern: The string or pattern to find within source | ||
replace: The replacement text | replace: The replacement text | ||
count: The number of occurences to replace, | count: The number of occurences to replace, 默认为 all. | ||
plain: | plain: 指示 pattern 参数是否视为"字符串"而非 Lua 风格的正则表达式的 Boolean 标记, 默认为 true。 | ||
]] | ]] | ||
function str.replace( frame ) | function str.replace( frame ) | ||
| 第450行: | 第383行: | ||
return string.rep( frame.args[1] or '', repetitions ) | return string.rep( frame.args[1] or '', repetitions ) | ||
end | end | ||
function str.split(inputstr, sep, no_pattern, ignore_null) | function str.split(inputstr, sep, no_pattern, ignore_null) | ||
--#invoke | --#invoke 支持 | ||
if type(inputstr) == type({table}) then | if type(inputstr) == type({table}) then | ||
if not getArgs then getArgs = require('Module:Arguments').getArgs end | if not getArgs then getArgs = require('Module:Arguments').getArgs end | ||
| 第490行: | 第424行: | ||
length = mw.ustring.len(input_str) | length = mw.ustring.len(input_str) | ||
-- | --split函数起点 | ||
if no_pattern_flag then | 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, | 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, | ||
| 第532行: | 第466行: | ||
join | join | ||
合并所有非空参数;第一个参数是分隔符。 | |||
{{#invoke:String|join| | 用法: | ||
{{#invoke:String|join|分隔符|文字1|文字2|文字3}} | |||
]] | ]] | ||
function str.join(frame) | function str.join(frame) | ||
| 第575行: | 第510行: | ||
--[[ | --[[ | ||
处理报错信息的辅助函数。 | |||
]] | ]] | ||
function str._error( error_str ) | function str._error( error_str ) | ||
local frame = mw.getCurrentFrame(); | local frame = mw.getCurrentFrame(); | ||
local error_category = frame.args.error_category or ' | local error_category = frame.args.error_category or '字符串模块报告的错误'; | ||
local ignore_errors = frame.args.ignore_errors or false; | local ignore_errors = frame.args.ignore_errors or false; | ||
local no_category = frame.args.no_category or false; | local no_category = frame.args.no_category or false; | ||
| 第587行: | 第522行: | ||
end | end | ||
local error_str = '<strong class="error"> | local error_str = '<strong class="error">字符串模块出错:' .. error_str .. '</strong>'; | ||
if error_category ~= '' and not str._getBoolean( no_category ) then | if error_category ~= '' and not str._getBoolean( no_category ) then | ||
error_str = '[[Category:' .. error_category .. ']]' .. error_str; | error_str = '[[Category:' .. error_category .. ']]' .. error_str; | ||
| 第596行: | 第531行: | ||
--[[ | --[[ | ||
解析 boolean strings 的辅助函数。 | |||
]] | ]] | ||
function str._getBoolean( boolean_str ) | function str._getBoolean( boolean_str ) | ||
| 第612行: | 第547行: | ||
boolean_value = boolean_str; | boolean_value = boolean_str; | ||
else | else | ||
error( ' | error( '找不到布尔值' ); | ||
end | end | ||
return boolean_value | return boolean_value | ||
| 第618行: | 第553行: | ||
--[[ | --[[ | ||
转换正则表达式字符,使之能作为普通字符串解析的辅助函数。 | |||
]] | ]] | ||
function str._escapePattern( pattern_str ) | function str._escapePattern( pattern_str ) | ||