Module:String/doc: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
no edit summary
No edit summary
 
(33 intermediate revisions by 19 users not shown)
Line 1:
{{High-risk|6332978all-pages=yes}}
<includeonly>{{Being deleted|2019 February 23|Module:PatternCount}}</includeonly><includeonly>{{Being deleted|2019 February 17|Module:String}}
{{Used in system}}
{{Being deleted|2019 February 18|Module:Text_count}}
{{Being deleted|2019 February 18|Module:Str_endswith}}</includeonly>
{{High-risk|6332978}}
{{Module rating|protected}}
 
{{Lmd|String}}
 
Line 125 ⟶ 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.
; ignore_errors
: If no match is found and ignore_errors=true, output an empty string rather than an error.
 
If the match_number or start_index are out of range for the string being queried, then this function generates an error. An error is also generated if no match is found.
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 }}
* <code><nowiki>{{#invoke:String|match|s= 0012001200 |pattern= 0*(%d*) }}</nowiki></code> → {{#invoke:String|match|s= 0012001200 |pattern= 0*(%d*) }}
 
== pos ==
Line 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 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 282:
* <code><nowiki>"{{#invoke:String|replace|source= abc123def456 |pattern= %d+ |replace= N |count=1 |plain= false }}"</nowiki></code> → "{{#invoke:String|replace|source= abc123def456 |pattern= %d+ |replace= N |count=1 |plain= false }}"
* <code><nowiki>"{{#invoke:String|replace|source= abc123def456 |pattern= %d+ |replace= N |plain= false }}"</nowiki></code> → "{{#invoke:String|replace|source= abc123def456 |pattern= %d+ |replace= N |plain= false }}"
* <code><nowiki>{{#invoke:String|replace|source= 0012001200 |pattern= ^0* |plain= false }}</nowiki></code> → {{#invoke:String|replace|source= 0012001200 |pattern= ^0* |plain= false }}
 
== rep ==
Line 302 ⟶ 303:
* <code><nowiki>"{{#invoke:String|rep|hello|3}}"</nowiki></code> → "{{#invoke:String|rep|hello|3}}"
* <code><nowiki>"{{#invoke:String|rep| hello | 3 }}"</nowiki></code> → "{{#invoke:String|rep| hello | 3 }}"
 
== escapePattern ==
 
Escapes magic characters in a [[mw:LUAREF#Patterns|Lua pattern]]. If you convert a string using this function, then the converted string will be interpreted as a literal string in functions that accept Lua patterns. For example, <code>.</code>, meaning "any character", will be converted to <code>%.</code>, meaning the literal character ".".
 
Usage:
 
* <code><nowiki>{{#invoke:String|escapePattern|</nowiki>''pattern_string''<nowiki>}}</nowiki></code>
 
Parameters:
 
; pattern_string
: The pattern string to escape
 
Examples:
 
* <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