MCBBS Wiki欢迎您共同参与编辑!在参与编辑之前请先阅读Wiki方针

如果在编辑的过程中遇到了什么问题,可以去讨论板提问。

为了您能够无阻碍地参与编辑 未验证/绑定过邮箱的用户,请尽快绑定/验证

MCBBS Wiki GitHub群组已上线!

您可以在回声洞中发表吐槽!

服务器状态监控。点击进入

本站由MCBBS用户自行搭建,与MCBBS及东银河系漫游指南(北京)科技有限公司没有从属关系。点此了解 MCBBS Wiki 不是什么>>

模块:String:修订间差异

跳到导航 跳到搜索
删除2,339字节 、​ 2022年10月6日 (星期四)
更新“模块:String”并翻译部分内容 // 维基盐编辑器
(创建页面,内容为“--[[ 引自维基百科(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…”)
 
(更新“模块:String”并翻译部分内容 // 维基盐编辑器)
 
第1行: 第1行:
-- 此模块引用自维基百科同名页面,引用行为依据 CC BY-SA 3.0 条款
-- 部分内容因为本地化或其他修改,可能与原模块有出入
--[[   
--[[   


引自维基百科(enwp:Module:String,oldid=552254999)
该模块提供了基本的字符串函数。


This module is intended to provide access to basic string functions.
这里的大多数函数都能以命名参数、匿名参数或混合前两种的形式调用。
若使用命名参数,Mediawiki 将自动删除参数前后的空格符号。
是否应该删除或保留前后空格,取决于具体用途。


Most of the functions provided here can be invoked with named parameters,
全局配置
unnamed parameters, or a mixture.  If named parameters are used, Mediawiki will
     ignore_errors: 如果设为 'true' 或 1,任何出错都将返回空字符串而非报错信息。
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 whitespace.
 
Global options
     ignore_errors: If set to 'true' or 1, any error condition will result in
        an empty string being returned rather than an error message. 
       
    error_category: If an error occurs, specifies the name of a category to
        include with the error message.  The default category is 
        [Category:Errors reported by Module String].
       
    no_category: If set to 'true' or 1, no category will be added if an error
        is generated.
          
          
Unit tests for this module are available at Module:String/tests.
    error_category: 如果发生异常,报错信息会自动添加一个分类到页面上,默认分类是 
----
        [Category:字符串模块报告的错误]。
该模块旨在提供对基本字符串函数的访问。
 
这里提供的大多数函数都可以用命名参数调用,
未命名的参数或混合着用。 如果使用命名参数,媒体维基将会
从参数中自动删除任何前部或者尾部的空格符号。
取决于预期的用途,保存或可能是有利的
删除这样的空白。
 
全局选项
    re_errors:如果设置为'true'或1,则会导致任何错误情况符串而不是错误消息。
   
    r_category:如果发生错误,请指定要分类的名称消息。 默认分类是 [Category:Errors reported by Module String]。(类别:模块字符串报告的错误)
          
          
     no_category:如果设置为'true'或1,如果发生错误,则不会添加任何分类 生成。
     no_category: 如果设为 'true' 或 1,报错时不会添加分类。
          
          
这个模块的测试单元可以在Module:String / tests下找到。
你可以在 Module:String/tests 找到单元测试。
]]
]]


第47行: 第25行:
len
len


This function returns the length of the target string.
返回目标字符串的长度。
 
Usage:
{{#invoke:String|len|target_string|}}
OR
{{#invoke:String|len|s=target_string}}
 
Parameters
    s: The string whose length to report
 
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from the target string. 
----
LEN


该函数返回目标字符串的长度。
用法:
{{#invoke:String|len|目标字符串|}}
{{#invoke:String|len|s=目标字符串}}


用法:
参数:
{{#invoke:String|len|target_string|}}
    s: 要计算长度的字符串
要么
{{#invoke:String|len|s=target_string}}


参数
若使用命名参数(第二种调用方法),Mediawiki 将自动删除目标字符串前后的空格符号。
    s:要报告的字符串长度
 
如果使用命名参数调用,媒体维基将自动删除任何前部或
后部目标字符串的空格。
]]
]]
function str.len( frame )
function str.len( frame )
第84行: 第46行:
sub
sub


This function returns a substring of the target string at specified indices.
根据指定索引,从目标字符串中获取子字符串


Usage:
用法:
{{#invoke:String|sub|target_string|start_index|end_index}}
{{#invoke:String|sub|目标字符串|开始位置|结束位置}}
OR
{{#invoke:String|sub|s=target_string|i=start_index|j=end_index}}
{{#invoke:String|sub|s=目标字符串|i=开始位置|j=结束位置}}
 
Parameters
    s: The string to return a subset of
    i: The fist index of the substring to return, defaults to 1.
    j: The last index of the string to return, defaults to the last character.
   
The first character of the string is assigned an index of 1.  If either i or j
is a negative value, it is interpreted the same as selecting a character by
counting from the end of the string.  Hence, a value of -1 is the same as
selecting the last character of the string.
 
If the requested indices are out of range for the given string, an error is
reported.
----


該函數返回指定索引處目標字符串的子字符串。
参数:
 
    s: 目标字符串,子字符串来自于它。
用法:
    i: 子字符串的开始位置,默认为 1。
{{#invoke:字符串|分| target_string| START_INDEX| END_INDEX}}
    j: 子字符串的结束位置,默认为最后一位。
要么
{{#invoke:字符串|子| S= target_string| I= START_INDEX| J = END_INDEX}}
 
參數
    s:返回一個子集的字符串
    i:要返回的子字符串的第一個索引,默認為1。
    j:要返回的字符串的最後一個索引,默認為最後一個字符。
      
      
字符串的第一個字符被分配索引1.如果i或j
字符串第一个字符的索引为 1。
是一個負值,它被解釋為與通過選擇一個字符相同
如果 i 或 j 传入了负数,将视为从字符串末尾开始计数的索引。
從字符串的末尾開始計數。 因此,-1的值與1相同
比如 -1 会视为字符串的最后一位。
選擇字符串的最後一個字符。


如果請求的索引超出給定字符串的範圍,則會出現錯誤
如果索引位置超出字符串长度,将会报错。
報導。
]]
]]
function str.sub( frame )
function str.sub( frame )
第134行: 第72行:
     local len = mw.ustring.len( s );
     local len = mw.ustring.len( s );


     -- Convert negatives for range checking
     -- 转换负数并检查区间
     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( 'String subset index out of range' );
         return str._error( '截取字符串索引脱离区间' );
     end
     end
     if j < i then
     if j < i then
         return str._error( 'String subset indices out of order' );
         return str._error( '截取字符串指示脱离顺序' );
     end
     end
      
      
第165行: 第103行:
match
match


This function returns a substring from the source string that matches a
根据指定模式,从目标字符串中获取匹配的子字符串
specified pattern.


Usage:
用法:
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
{{#invoke:String|match|目标字符串|pattern_string|start_index|match_number|plain_flag|无匹配输出}}
OR
{{#invoke:String|pos|s=source_string|pattern=pattern_string|start=start_index
{{#invoke:String|pos|s=目标字符串|pattern=pattern_string|start=start_index
     |match=match_number|plain=plain_flag|nomatch=nomatch_output}}
     |match=match_number|plain=plain_flag|nomatch=无匹配输出}}


Parameters
参数:
     s: The string to search
     s: 目标字符串,以它查找匹配
     pattern: The pattern or string to find within the string
     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.  Defaults to 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.  Defaults to 1.
         the last match.  默认为 1。
     plain: A flag indicating that the pattern should be understood as plain
     plain: 指示 pattern 参数是否视为"字符串"的标记,默认为否(即默认视为"模式")。
        text.  Defaults to false.
     nomatch: 如果找不到匹配项,输出 "nomatch" 的值而非报错。
     nomatch: If no match is found, output the "nomatch" value rather than an error.


If invoked using named parameters, Mediawiki will automatically remove any leading or
若使用命名参数(第二种调用方法),Mediawiki 将自动删除参数前后的空格符号。
trailing whitespace from each string.  In some circumstances this is desirable, in  
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.


For information on constructing Lua patterns, a form of [regular expression], see:
关于构造 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( 'Target string is empty' );
         return str._error( '目标字符串是空的' );
     end
     end
     if pattern == '' then
     if pattern == '' then
         return str._error( 'Pattern string is empty' );
         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( 'Requested start is out of range' );
         return str._error( '要求的起始点脱离区间' );
     end
     end
     if match_index == 0 then
     if match_index == 0 then
         return str._error( 'Match index is out of range' );
         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( 'Match not found' );
             return str._error( '找不到匹配' );
         else
         else
             return nomatch;
             return nomatch;
第275行: 第210行:
pos
pos


This function returns a single character from the target string at position pos.
返回目标字符串在索引位置的那个字符。


Usage:
用法:
{{#invoke:String|pos|target_string|index_value}}
{{#invoke:String|pos|目标字符串|索引值}}
OR
OR
{{#invoke:String|pos|target=target_string|pos=index_value}}
{{#invoke:String|pos|target=目标字符串|pos=索引值}}


Parameters
参数:
     target: The string to search
     target: 目标字符串,从它获取字符
     pos: The index for the character to return
     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.


The first character has an index value of 1.
字符串第一个字符的索引为 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( 'String index out of range' );
         return str._error( '字符串索引脱离区间' );
     end     
     end     
      
      
第346行: 第281行:
string.
string.


Usage:
用法:
{{#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}}


Parameters
参数:
     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, defaults to 1
     start: The index within the source string to start the search, 默认为 1
     plain: Boolean flag indicating that target should be understood as plain
     plain: 指示 pattern 参数是否视为"字符串"而非 Lua 风格的正则表达式的 Boolean 标记, 默认为 true。
        text and not as a Lua style regular expression, defaults to 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.


Usage:
用法:
{{#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}}


Parameters
参数:
     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, defaults to all.
     count: The number of occurences to replace, 默认为 all.
     plain: Boolean flag indicating that pattern should be understood as plain
     plain: 指示 pattern 参数是否视为"字符串"而非 Lua 风格的正则表达式的 Boolean 标记, 默认为 true。
        text and not as a Lua style regular expression, defaults to 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函數起點
--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


Join all non empty arguments together; the first argument is the separator.
合并所有非空参数;第一个参数是分隔符。
Usage:
 
{{#invoke:String|join|sep|one|two|three}}
用法:
{{#invoke:String|join|分隔符|文字1|文字2|文字3}}
]]
]]
function str.join(frame)
function str.join(frame)
第575行: 第510行:


--[[
--[[
Helper function to handle error messages.
处理报错信息的辅助函数。
]]
]]
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 'Errors reported by Module String';
     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">String Module Error: ' .. error_str .. '</strong>';
     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行:


--[[
--[[
Helper Function to interpret boolean strings
解析 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( 'No boolean value found' );
         error( '找不到布尔值' );
     end     
     end     
     return boolean_value
     return boolean_value
第618行: 第553行:


--[[
--[[
Helper function that escapes all pattern characters so that they will be treated
转换正则表达式字符,使之能作为普通字符串解析的辅助函数。
as plain text.
]]
]]
function str._escapePattern( pattern_str )
function str._escapePattern( pattern_str )
我们提供服务需要使用Cookie。您使用我们的服务,即表示您同意我们使用Cookie。

导航菜单