Module:String/doc: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
no edit summary
(add documentation for escapePattern)
No edit summary
 
(25 intermediate revisions by 17 users not shown)
Line 1:
{{High-risk|6332978all-pages=yes}}
{{Tfm/dated|page=String/doc|otherpage=Join|link=Wikipedia:Templates for discussion/Log/2019 April 16#Module:String|type=module|bigbox=yes}}
{{High-risk|6332978}}
{{Used in system}}
{{Module rating|protected}}
 
{{Lmd|String}}
 
Line 123 ⟶ 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 145 ⟶ 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 231 ⟶ 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 252 ⟶ 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 323 ⟶ 321:
* <code><nowiki>"{{#invoke:String|escapePattern|A.D.}}"</nowiki></code> → "{{#invoke:String|escapePattern|A.D.}}"
* <code><nowiki>"{{#invoke:String|escapePattern|10%}}"</nowiki></code> → "{{#invoke:String|escapePattern|10%}}"
== count ==
Counts the number of times a given pattern appears in the arguments that get passed on to this module.
 
Usage:
: <code><nowiki>{{#invoke:</nowiki>String|count|''source_str''|''pattern_string''|''plain_flag''}}</code>
 
OR
 
: <code><nowiki>{{#invoke:</nowiki>String|count|source= ''source_string'' |pattern= ''pattern_string''|plain= ''plain_flag'' }}</code>
Parameters:
; source_string
: The string to count occurrences in
; pattern
: The string or pattern to count occurrences of within source
; 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 true
 
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 ==
Joins all strings passed as arguments into one string, treating the first argument as a separator
 
Usage:
: <code><nowiki>{{#invoke:</nowiki>String|join|''separator''|''string1''|''string2''|...}}</code>
Parameters:
; separator
: String that separates each string being joined together
: Note that leading and trailing spaces are ''not'' stripped from the separator.
; string1/string2/...
: Strings being joined together
Examples:
* <code><nowiki>"{{#invoke:String|join|x|foo|bar|baz}}"</nowiki></code> → "{{#invoke:String|join|x|foo|bar|baz}}"
* <code><nowiki>"{{#invoke:String|join||a|b|c|d|e|f|g}}"</nowiki></code> → "{{#invoke:String|join||a|b|c|d|e|f|g}}"
* <code><nowiki>"{{#invoke:String|join|,|a|b|c|d|e|f|g}}"</nowiki></code> → "{{#invoke:String|join|,|a|b|c|d|e|f|g}}"
* <code><nowiki>"{{#invoke:String|join|, |a|b|c|d|e|f|g}}"</nowiki></code> → "{{#invoke:String|join|, |a|b|c|d|e|f|g}}"
* <code><nowiki>"{{#invoke:String|join| &ndash; |a|b|c|d|e|f|g}}"</nowiki></code> → "{{#invoke:String|join| &ndash; |a|b|c|d|e|f|g}}"
The preceding example uses the html entity &amp;ndash; but the unicode character also works.
 
== endswith ==
Usage:
: <code><nowiki>{{#invoke:</nowiki>String|endswith|''source_str''|''pattern_string''}}</code>
 
OR
 
: <code><nowiki>{{#invoke:</nowiki>String|endswith|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.
* [[Module:StringFunc]]
* [[Module:Str endswith]]
* [[Module:Ustring]]
 
0

edits

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

Navigation menu