Module:String/doc: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
no edit summary
(→‎join: expand to explain about leading and trailing spaces in the separator; give examples)
No edit summary
 
(20 intermediate revisions by 15 users not shown)
Line 1:
{{High-risk|6332978all-pages=yes}}
{{Used in system}}
{{Module rating|protected}}
 
{{Lmd|String}}
 
Line 122 ⟶ 121:
: 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. Defaults to 1.
; plain
: Boolean flag indicating that pattern should be understood as plain text and not as a [[mw:Extension:Scribunto/Lua reference manual#Ustring patterns|Scribunto ustring pattern]] (a unicode-friendly [[w:en:Lua (programming language)|Lua]]-style [[w:en:regular expression|regular expression]]). Defaults to false (to change: <code>plain=true</code>)
; nomatch
: If no match is found, output the "nomatch" value rather than an error.
Line 144 ⟶ 143:
* <code><nowiki>{{#invoke:String|match|s= abc123%d+ |pattern= %d+ }}</nowiki></code> → {{#invoke:String|match|s= abc123%d+ |pattern= %d+ }}
* <code><nowiki>{{#invoke:String|match|s= abc123%d+ |pattern= %d+ |plain= true }}</nowiki></code> → {{#invoke:String|match|s= abc123%d+ |pattern= %d+ |plain= true }}
* <code><nowiki>{{#invoke:String|match|s= abc |pattern= %d }}</nowiki></code> → {{#invoke:String|match|s= abc |pattern= %d |no_category=true}}
* <code><nowiki>{{#invoke:String|match|s= abc |pattern= %d |nomatch= No numeric characters in string }}</nowiki></code> → {{#invoke:String|match|s= abc |pattern= %d |nomatch= No numeric characters in string }}
* <code><nowiki>{{#invoke:String|match|s= abc |pattern= %d |ignore_errors= true }}</nowiki></code> → {{#invoke:String|match|s= abc |pattern= %d |ignore_errors= true }}
Line 230 ⟶ 229:
: The index within the source string to start the search, defaults to 1
; plain
: Boolean flag indicating that target should be understood as plain text and not as a [[mw:Extension:Scribunto/Lua reference manual#Ustring patterns|Scribunto ustring pattern]] (a unicode-friendly [[w:en:Lua (programming language)|Lua]]-style [[w:en:regular expression|regular expression]],); defaults to true
 
This function returns the first index >= "start" where "target" can be found within "source". Indices are 1-based. If "target" is not found, then this function returns 0. If either "source" or "target" are missing / empty, this function also returns 0.
Line 251 ⟶ 250:
* <code><nowiki>{{#invoke:String|find|source= abc123def |target=c|plain=false}}</nowiki></code> → {{#invoke:String|find|source= abc123def |target=c|plain=false}}
 
== replace (gsub) ==
 
This function allows one to replace a target string or pattern within another string. To Lua programmers: this function works internally by calling {{code|string.gsub}}.
 
Usage:
Line 340 ⟶ 339:
 
Examples:
* Count of 'a': <code><nowiki>"{{#invoke:String|count|aabbcc|a}}"</nowiki></code> → "{{#invoke:String|count|aabbcc|a}}"
* Count of "either 'a' or 'c' ":<code><nowiki>"{{#invoke:String|count|aabbcc|[ac]|plain=false}}"</nowiki></code> → "{{#invoke:String|count|aabbcc|[ac]|plain=false}}"
* Count of "not 'a' ": <code><nowiki>"{{#invoke:String|count|aaabaaac|[^a]|plain=false}}"</nowiki></code> → "{{#invoke:String|count|aaabaaac|[^a]|plain=false}}"
* Count of "starts with 'a' ": <code><nowiki>"{{#invoke:String|count|aaabaaac|^a|plain=false}}"</nowiki></code> → "{{#invoke:String|count|aaabaaac|^a|plain=false}}"
 
== join ==
Line 364 ⟶ 365:
== endswith ==
Usage:
: <code><nowiki>{{#invoke:</nowiki>String|countendswith|''source_str''|''pattern_string''}}</code>
 
OR
 
: <code><nowiki>{{#invoke:</nowiki>String|countendswith|source= ''source_string'' |pattern= ''pattern_string''}}</code>
Returns "yes" if the source string ends with the pattern string. Both strings are trimmed before use.
 
* <code><nowiki>"{{#invoke:String|endswith|xxxyyy|y}}"</nowiki></code> → "{{#invoke:String|endswith|xxxyyy|y}}"
* <code><nowiki>"{{#invoke:String|endswith|xxxyyy|z}}"</nowiki></code> → "{{#invoke:String|endswith|xxxyyy|z}}"
 
== See also ==
* [[Module:String2]] for functions to convert the capitalisation of strings to upper, lower, sentence or title case.
0

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu