Module:String/doc: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Add examples for each function. Document usage of sublength and str_find.
m (→‎top: clean up, replaced: {{ml → {{Lm)
(Add examples for each function. Document usage of sublength and str_find.)
Line 1:
{{High-risk|over 5,040,000+.4 million}}<!-- Rounded transclusionfrom count. The exact transclusion count as of 2018-01-26 20:43 is: 50426095,426,501 -->
{{Module rating|protected}}
 
Line 35:
; s
: The string whose length to report
 
Examples:
 
Example* <code><nowiki>{{#invoke:String|replen|hello|3 abcdefghi }}</nowiki></code> gives {{#invoke:String|replen|hello|3 abcdefghi }}
* <code><nowiki>{{#invoke:String|len|s= abcdefghi }}</nowiki></code> → {{#invoke:String|len|s= abcdefghi }}
 
== sub ==
 
: This function returns a substring of the target string at specified indices.
 
Usage:
Line 60 ⟶ 65:
 
If the requested indices are out of range for the given string, an error is reported.
 
Examples:
 
* <code><nowiki>"{{#invoke:String|sub| abcdefghi }}"</nowiki></code> → "{{#invoke:String|sub| abcdefghi }}"
* <code><nowiki>"{{#invoke:String|sub|s= abcdefghi }}"</nowiki></code> → "{{#invoke:String|sub|s= abcdefghi }}"
* <code><nowiki>"{{#invoke:String|sub| abcdefghi | 3 }}"</nowiki></code> → "{{#invoke:String|sub| abcdefghi | 3 }}"
* <code><nowiki>"{{#invoke:String|sub|s= abcdefghi |i= 3 }}"</nowiki></code> → "{{#invoke:String|sub|s= abcdefghi |i= 3 }}"
* <code><nowiki>"{{#invoke:String|sub| abcdefghi | 3 | 4 }}"</nowiki></code> → "{{#invoke:String|sub| abcdefghi | 3 | 4 }}"
* <code><nowiki>"{{#invoke:String|sub|s= abcdefghi |i= 3 |j= 4 }}"</nowiki></code> → "{{#invoke:String|sub|s= abcdefghi |i= 3 |j= 4 }}"
 
== sublength ==
 
This function implements the features of {{tl|Strstr sub old}} and is kept in order to maintain these older templates. It returns a substring of the target string starting at a specified index and of a specified length.
 
Usage:
 
: <code><nowiki>{{#invoke:</nowiki>String|sublength|s= ''target_string'' |i= ''start_index'' |len= ''length'' }}</code>
 
Parameters:
 
; s
: The string
; i
: The starting index of the substring to return. The first character of the string is assigned an index of 0.
; len
: The length of the string to return, defaults to the last character.
 
Examples:
 
* <code><nowiki>{{#invoke:String|sublength|s= abcdefghi }}</nowiki></code> → {{#invoke:String|sublength|s= abcdefghi }}
* <code><nowiki>{{#invoke:String|sublength|s= abcdefghi |i= 3 }}</nowiki></code> → {{#invoke:String|sublength|s= abcdefghi |i= 3 }}
* <code><nowiki>{{#invoke:String|sublength|s= abcdefghi |i= 3 |len= 4 }}</nowiki></code> → {{#invoke:String|sublength|s= abcdefghi |i= 3 |len= 4 }}
 
== match ==
Line 75 ⟶ 108:
OR
 
: <code><nowiki>{{#invoke:</nowiki>String|match|s= ''source_string'' |pattern= ''pattern_string'' |start= ''start_index'' |match= ''match_number'' |plain= ''plain_flag'' |nomatch= ''nomatch_output'' }}</code>
 
Parameters:
Line 99 ⟶ 132:
* [[mw:Extension:Scribunto/Lua_reference_manual#Patterns|Scribunto patterns]]
* [[mw:Extension:Scribunto/Lua_reference_manual#Ustring_patterns|Scribunto Unicode string patterns]]
 
Examples:
* <code><nowiki>{{#invoke:String|match| abc123def456 |%d+}}</nowiki></code> → {{#invoke:String|match| abc123def456 |%d+}}
* <code><nowiki>{{#invoke:String|match|s= abc123def456 |pattern= %d+ }}</nowiki></code> → {{#invoke:String|match|s= abc123def456 |pattern= %d+ }}
* <code><nowiki>{{#invoke:String|match| abc123def456 |%d+|6}}</nowiki></code> → {{#invoke:String|match| abc123def456 |%d+|6}}
* <code><nowiki>{{#invoke:String|match|s= abc123def456 |pattern= %d+ |start= 6 }}</nowiki></code> → {{#invoke:String|match|s= abc123def456 |pattern= %d+ |start= 6 }}
* <code><nowiki>{{#invoke:String|match|s= abc123def456 |pattern= %d+ |start= 6 |match= 2 }}</nowiki></code> → {{#invoke:String|match|s= abc123def456 |pattern= %d+ |start= 6 |match= 2 }}
* <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 }}
* <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 }}
 
== pos ==
Line 125 ⟶ 169:
 
A requested value of zero, or a value greater than the length of the string returns an error.
 
Examples:
 
* <code><nowiki>{{#invoke:String|pos| abcdefghi | 4 }}</nowiki></code> → {{#invoke:String|pos| abcdefghi | 4 }}
* <code><nowiki>{{#invoke:String|pos|target= abcdefghi |pos= 4 }}</nowiki></code> → {{#invoke:String|pos|target= abcdefghi |pos= 4 }}
 
== str_find ==
Line 132 ⟶ 181:
Returns the first index in "source" that is a match to "target". Indexing is 1-based, and the function returns -1 if the "target" string is not present in "source".
 
Important Note: If the "target" string is empty / missing, this function returns a value of "1", which is generally unexpected behavior, and must be accounted for separatetlyseparately.
 
Usage:
 
: <code><nowiki>{{#invoke:</nowiki>String|find|''source_string''|''target_string''}}</code>
 
OR
 
: <code><nowiki>{{#invoke:</nowiki>String|find|source= ''source_string'' |target= ''target_string'' }}</code>
 
Parameters:
 
; source
: The string to search
; target
: The string to find within source
 
Examples:
* <code><nowiki>{{#invoke:String|str_find| abc123def }}</nowiki></code> → {{#invoke:String|str_find| abc123def }}
* <code><nowiki>{{#invoke:String|str_find|source= abc123def }}</nowiki></code> → {{#invoke:String|str_find|source= abc123def }}
* <code><nowiki>{{#invoke:String|str_find| abc123def |123}}</nowiki></code> → {{#invoke:String|str_find| abc123def |123}}
* <code><nowiki>{{#invoke:String|str_find|source= abc123def |target= 123 }}</nowiki></code> → {{#invoke:String|str_find|source= abc123def |target= 123 }}
 
== find ==
Line 140 ⟶ 210:
Usage:
 
: <code><nowiki>{{#invoke:</nowiki>String|find|''source_strsource_string''|''target_string''|''start_index''|''plain_flag''}}</code>
 
OR
 
: <code><nowiki>{{#invoke:</nowiki>String|find|source= ''source_strsource_string'' |target= ''target_strtarget_string'' |start= ''start_index'' |plain= ''plain_flag'' }}</code>
 
Parameters:
Line 160 ⟶ 230:
 
This function should be safe for UTF-8 strings.
 
Examples:
* <code><nowiki>{{#invoke:String|find| abc123def }}</nowiki></code> → {{#invoke:String|find| abc123def }}
* <code><nowiki>{{#invoke:String|find|source= abc123def }}</nowiki></code> → {{#invoke:String|find|source= abc123def }}
* <code><nowiki>{{#invoke:String|find| abc123def |123}}</nowiki></code> → {{#invoke:String|find| abc123def |123}}
* <code><nowiki>{{#invoke:String|find|source= abc123def |target= 123 }}</nowiki></code> → {{#invoke:String|find|source= abc123def |target= 123 }}
* <code><nowiki>{{#invoke:String|find| abc123def |%d|3|false}}</nowiki></code> → {{#invoke:String|find| abc123def |%d|3|false}}
* <code><nowiki>{{#invoke:String|find|source= abc123def |target= %d |start= 3 |plain= false }}</nowiki></code> → {{#invoke:String|find|source= abc123def |target= %d |start= 3 |plain= false }}
 
== replace ==
Line 171 ⟶ 249:
OR
 
: <code><nowiki>{{#invoke:</nowiki>String|replace|source= ''source_string'' |pattern= ''pattern_string'' |replace= ''replace_string'' |count= ''replacement_count'' |plain= ''plain_flag'' }}</code>
 
Parameters:
Line 185 ⟶ 263:
; 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:
 
* <code><nowiki>"{{#invoke:String|replace| abc123def456 |123|N}}"</nowiki></code> → "{{#invoke:String|replace| abc123def456 |123|N}}"
* <code><nowiki>"{{#invoke:String|replace|source= abc123def456 |pattern= 123 |replace= N }}"</nowiki></code> → "{{#invoke:String|replace|source= abc123def456 |pattern= 123 |replace= N }}"
* <code><nowiki>"{{#invoke:String|replace| abc123def456 |%d+|N|1|false}}"</nowiki></code> → "{{#invoke:String|replace| abc123def456 |%d+|N|1|false}}"
* <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 }}"
 
== rep ==
 
Repeats a string ''n'' times. A simple function to pipe string.rep to templates.
 
Usage:
 
Line 200 ⟶ 287:
: The number of repetitions.
 
Examples:
Example <code><nowiki>{{#invoke:String|rep|hello|3}}</nowiki></code> gives {{#invoke:String|rep|hello|3}}
 
* <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 }}"
 
== 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]]
 
<includeonly>{{Sandbox other||
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu