模板:Str mid
- {{Str mid}}
- {{Str mid/core}}
- {{Str mid/testcases}}
![]() | 此模板使用Lua語言: |
![]() | 此模板被引用於約1,110,000個頁面,佔全部頁面的14%。 為了避免造成大規模的影響,所有對此模板的編輯應先於沙盒、測試樣例或您的沙盒上測試。 測試後無誤的版本可以一次性地加入此模板中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
Usage
Characters in the source string are counted: the first is index 1
, the last one is index length of string
. The substring requested can be defined by these two indexes. Also, the substring can be defined by one index, and the substring length. The options are:
{{str mid|<string>|<index of first character>|<length of substring>|<index of last character>}}
It returns a part of the <string>, starting with character on position index, length <sublength>, last character returned is end index.
- {{str mid|Abcdefghijklmnopqrstuvwxyz|5|3}} → efg
If parameter 2 is omitted, start is from character 1:
- {{str mid|Abcdefghijklmnopqrstuvwxyz||5}} → Abcde
If parameter 3 is omitted, the whole rest of the string is returned:
- {{str mid|Abcdefghijklmnopqrstuvwxyz|15}} → opqrstuvwxyz
Counting from the end
By entering a negative value, the index is counted from the end:
- {{str mid|Abcdefghijklmnopqrstuvwxyz|-7|4}} → tuvw
- {{str mid|Abcdefghijklmnopqrstuvwxyz|-7}} → tuvwxyz
Instead if substring length, use index for last character
By using parameter |4=n
, a number, you do not ask for substring length, but the last character (as counted from the start of the input string):
- {{str mid|Abcdefghijklmnopqrstuvwxyz|5||7}} → efg
Spaces are kept
In the returned substring, spaces are kept:
- >{{str mid|I like this.|2|6}}< → >like<
But the input string is trimmed:
- >{{str mid| I like this. |2|6}}< → >like<
Error messages
When the ranges (either param2 or the substring length) is out impossible in the string, an error is returned:
- {{str mid|Abcdefghijklmnopqrstuvwxyz|30}} → 字符串模塊出錯:截取字符串索引脫離區間
- {{str mid|Abcdefghijklmnopqrstuvwxyz|0|45}} → efghijklmnopqrstuvwxyz
The error can be suppressed by using |ignore errors=true
. A blank (empty string) will be returned:
- {{str mid|Abcdefghijklmnopqrstuvwxyz|0|45|ignore errors=true}} → efghijklmnopqrstuvwxyz
Error category
|no category=true
switches off category adding.|error category=[[Category:Your category name]]
sets the category to be added when in case of an error.
- Default is Category:Errors reported by Module String.
See also
|
上述文件嵌入自Template:Str mid/doc。 (編輯 | 歷史) 編者可以在本模板的沙盒 (建立 | 鏡像)和測試樣例 (建立)頁面進行實驗。 請在/doc子頁面中添加分類。本模板的子頁面。 |