跳转到内容

模組討論:String

页面内容不支持其他语言。
维基百科,自由的百科全书

編輯請求[编辑]

请求已处理

請求新增一個切割字串函數,已在Module:Chemicals中使用(Special:Diff/47444448函式原型: string[] str::split(string,string))
Module:Chemicals應該是要放置化學相關函數
而字串相關函數應置於此,因此申請編輯請求
function str.split(inputstr, sep)
        if sep == nil then
                sep = "%s"
        end
        local result={} ; i=1
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                result[i] = str
                i = i + 1
        end
        return result
end

lua沒有函數載入順序問題,以上函式放在任意位置皆可 -- 宇帆(明年二月加入維基將滿十周年!留言·歡迎簽到·聯絡2017年12月19日 (二) 17:55 (UTC)[回复]

split[编辑]

function prototypt(by c++):string[] 模块::String::str::split ( const string inputstr, const string sep );
This function splits a String into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.

Usage:

str.split( target_string, separator_string )

Parameters:

inputstr target_string
The string to split
sep separator_string
Specifies the string which denotes the points at which each split should occur.
function p.split(inputstr, sep, no_pattern, ignore_null)
	--#invoke 支援
	if type(inputstr) == type({table}) then
		if not getArgs then getArgs = require('Module:Arguments').getArgs end
		args = getArgs(inputstr, {parentFirst=true})
		for arg_name, arg_value in pairs( args ) do
			if arg_name == 1 or arg_name == '1' or arg_name == "str" or arg_name == "inputstr" or arg_name == "input" then
				input_str = arg_value
			elseif arg_name == 2 or arg_name == '2' or arg_name == "sep" or arg_name == "separator" then
				separ = arg_value
			elseif arg_name == 3 or arg_name == '3' or arg_name == "no_pattern" or arg_name == "no pattern" then
				no_pattern_flag = arg_value
			elseif arg_name == 4 or arg_name == '4' or arg_name == "ignore_null" or arg_name == "ignore null" then
				ignore_null_flag = arg_value
			elseif arg_name == 5 or arg_name == '5' or arg_name == "format" then
				format = arg_value or "*{{{1}}}\n";
			end
		end
		if not yesno then yesno = require('Module:Yesno') end
		no_pattern_flag = yesno( no_pattern_flag or 'yes' )
		ignore_null_flag = yesno( ignore_null_flag or 'no' )
		is_invoke = true
		format = mw.ustring.gsub(format or "*{{{1}}}\n", "%{%{%{.-%}%}%}", "%%s" );
		it = mw.ustring.find(format, "%%s", 1)
		if it == nil then format = format .. "%s" end
		format = mw.ustring.gsub(format, "\\n", "\n")
	else
		input_str = inputstr
		separ = sep
		no_pattern_flag = no_pattern
		ignore_null_flag = ignore_null
		is_invoke = false
	end
	input_str = input_str or '' 
	separ = separ or "%s"
	if no_pattern_flag == nil then no_pattern_flag = true end
	if ignore_null_flag == nil then ignore_null_flag = false end

	mw.log(no_pattern_flag, ignore_null_flag);
	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, 
			"%[", "%["), "%]", "%]"), "%{", "%{"), "%}", "%}"), "%%", "%%%%"), "%)", "%)"), "%-", "%-"),
			"%^", "%^"), "%$", "%$"), "%(", "%("), "%.", "%."), "%*", "%*"), "%+", "%+"), "%|", "%|");
	end
	iterator = 1 ; i = 1 ; flag = true
	result = {}
	separ_str_begin, separ_str_end = mw.ustring.find(input_str, separ, iterator)
	--
	debug1 = 1
	--
	while flag do
		debug1 = debug1 + 1
		if separ_str_begin == nil or iterator > length or debug1 >= 100 then
			separ_str_begin = 0
			separ_str_end = -2
			flag = false
		end
		if separ_str_end < separ_str_begin then separ_str_end = separ_str_begin end
		mw.log(separ_str_begin, separ_str_end, iterator)
		finded_str = mw.ustring.sub(input_str, iterator, separ_str_begin - 1)
		if not(mw.text.trim(finded_str) == '' and ignore_null_flag) then
			result[i] = finded_str
			mw.log("\"" .. result[i] .. "\"")
			i = i + 1
		end

		iterator = separ_str_end + 1
		separ_str_begin, separ_str_end = mw.ustring.find(input_str, separ, iterator)
	end
	if is_invoke then
		body = ''
		for i, result_str in pairs( result ) do
			body = body .. mw.ustring.gsub(format, "%%s", result_str)
		end
		return body
	end
	return result;
end
以及測試結果

測試1[编辑]

  • ---123-45-6------
    • 123
    • 45
    • 6


測試2[编辑]

  • 123娜娜奇456娜娜奇789娜娜奇娜娜奇910娜娜娜娜奇奇123奇娜娜奇
    • 123
    • 456
    • 789
    • 910娜娜
    • 奇123奇


測試3[编辑]

  • 123娜娜奇456娜娜奇789娜娜奇娜娜奇910娜娜娜娜奇奇123奇娜奇
    • 123娜
    • 456娜
    • 789娜
    • 910娜娜娜
    • 奇123奇


測試4[编辑]

  • 123娜娜奇456娜娜奇789娜娜奇娜娜奇910娜娜娜娜奇奇123奇娜娜
    • 123
    • 奇456
    • 奇789
    • 奇910
    • 奇奇123奇


測試5[编辑]

  • 123娜娜奇456娜娜奇789娜娜奇娜娜奇910娜娜娜娜奇奇123奇
    • 123
    • 奇456
    • 奇789
    • 奇910
    • 奇奇123奇

編輯請求[编辑]

请求已处理--Xiplus#Talk 2018年10月20日 (六) 12:36 (UTC)[回复]

修訂內容:與此修訂相同:wikiversity:zh:special:diff:50823:模块:String
理由:刪除已經用不到的偵錯用log輸出。
  • (~)補充:已確認CAS號辨識函數已佈署到所有化學品訊息框模板,且截至今天為止未發生錯誤,因此偵錯用log輸出已不再需要,應予以移除。

-- 宇帆(維基貢獻十周年!留言·歡迎簽到[試用小工具]2018年8月17日 (五) 12:27 (UTC)[回复]

String Join[编辑]

请求已处理--Xiplus#Talk 2020年2月26日 (三) 00:41 (UTC)[回复]

Hi, copied the content in Template:College color list from English wiki and they said 腳本錯誤:函數「join」不存在。

So is it possible to add the join features from English wiki? Thanks

--[[
join

Join all non empty arguments together; the first argument is the separator.
Usage:
{{#invoke:String|join|sep|one|two|three}}
]]
function str.join(frame)
	local args = {}
	local sep
	for _, v in ipairs( frame.args ) do
		if sep then
			if v ~= '' then
				table.insert(args, v)
			end
		else
			sep = v
		end
	end
	return table.concat( args, sep or '' )
end

Winston留言2020年2月19日 (三) 03:00 (UTC)[回复]

編輯請求 2020-09-01[编辑]

请求已拒绝

Module:String/draft,新增enwiki的count功能。 2020年9月1日 (二) 16:35 (UTC)[回复]

提供的版本將部分錯誤訊息改成了英文,請修正後重開請求。--Xiplus#Talk 2020年9月30日 (三) 08:29 (UTC)[回复]

編輯請求 2020-10-13[编辑]

请求已拒绝--Xiplus#Talk 2020年10月30日 (五) 05:06 (UTC)[回复]

編輯請求 2020-09-01的要求已完成,請複查。 2020年10月13日 (二) 18:49 (UTC)[回复]

Xiplus 2020年10月13日 (二) 18:50 (UTC)[回复]

count[编辑]

是否可以添加enwiki的“count(计数)”功能?--Bouzinac留言2021年3月26日 (五) 14:19 (UTC)[回复]