Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Search
Search
English
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Module:String/doc/en
Module
Discussion
English
Read
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
<noinclude><!-- -->{{#ifeq:{{SUBPAGENAME}}|doc||{{Documentation subpage}}}}<!-- --><languages/><!-- --></noinclude>{{#switch: | = <includeonly>{{Languages|Module:String/doc}}</includeonly> <!-- Add categories where indicated at the bottom of this page and interwikis at Wikidata --> {{Magic words|general=1}} {{Module rating|release}} {{Module rating|protected}} This module is intended to provide access to basic string functions. Most of the functions provided here can be invoked with named parameters, unnamed parameters, or a mixture. If named parameters are used, MediaWiki will automatically remove any leading or trailing whitespace from the parameter. Depending on the intended use, it may be advantageous to either preserve or remove such whitespace. == Global options == ; <code>ignore_errors</code> : If set to {{phpi|true}} or 1, any error condition will result in an empty string being returned rather than an error message. ; <code>error_category</code> : If an error occurs, specifies the name of a category to include with the error message. The default category is {{ll|Category:Errors reported by Module String}}. ; <code>no_category</code> : If set to {{phpi|true}} or 1, no category will be added if an error is generated. Unit tests for this module are available at [[Module:String/testcases]]. == <code>len</code> == This function returns the length of the target string. Usage: : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}len{{!}}''$1''<nowiki>}}</nowiki></code> |1=target_string }} or : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}len{{!}}s= ''$1'' <nowiki>}}</nowiki></code> |1=target_string }} Parameters: ; <code>s</code> : The string whose length to report == <code>sub</code> == : This function returns a substring of the target string at specified indices. Usage: : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}sub{{!}}''$1''{{!}}''$2''{{!}}''$3''<nowiki>}}</nowiki></code> |1=target_string |2=start_index |3=end_index }} OR : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}sub<wbr/>{{!}}s= ''$1'' {{!}}i= ''$2'' {{!}}j= ''$3'' <nowiki>}}</nowiki></code> |1=target_string |2=start_index |3=end_index }} Parameters: ; <code>s</code> : The string to return a subset of ; <code>i</code> : The first index of the substring to return, defaults to 1. ; <code>j</code> : The last index of the string to return, defaults to the last character. The first character of the string is assigned an index of 1. If either i or j is a negative value, it is interpreted the same as selecting a character by counting from the end of the string. Hence, a value of -1 is the same as selecting the last character of the string. If the requested indices are out of range for the given string, an error is reported. == <code>match</code> == This function returns a substring from the source string that matches a specified pattern. Usage: : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}match{{!}}''$1''{{!}}''$2''{{!}}''$3''{{!}}''$4''{{!}}''$5''{{!}}''$6''<nowiki>}}</nowiki></code> |1=source_string |2=pattern_string |3=start_index |4=match_number |5=plain_flag |6=nomatch_output }} OR : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}match{{!}}s= ''$1'' {{!}}pattern= ''$2'' {{!}}start= ''$3'' {{!}}match= ''$4'' {{!}}plain= ''$5'' {{!}}nomatch= ''$6'' <nowiki>}}</nowiki></code> |1=source_string |2=pattern_string |3=start_index |4=match_number |5=plain_flag |6=nomatch_output }} Parameters: ; <code>s</code> : The string to search ; <code>pattern</code> : The pattern or string to find within the string ; <code>start</code> : The index within the source string to start the search. The first character of the string has index 1. Defaults to 1. ; <code>match</code> : 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 <code>match = 1</code>. If a negative number is specified then a match is returned counting from the last match. Hence <code>match = -1</code> is the same as requesting the last match. Defaults to 1. ; <code>plain</code> : Boolean flag indicating that pattern should be understood as plain text and not as a [[w:Lua (programming language)|Lua]]-style [[w:Regular expression|regular expression]]. Defaults to {{phpi|false}}. ; <code>nomatch</code> : If no match is found, output the "nomatch" value rather than an error. If the <code>match</code> or <code>start</code> 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. If one adds the parameter <code>ignore_errors=true</code>, then the error will be suppressed and an empty string will be returned on any failure. For information on constructing [[w:Lua (programming language)|Lua]] patterns, a form of [[w:Regular expression|regular expression]], see: * [https://www.lua.org/manual/5.1/manual.html#5.4.1 Lua reference: Patterns] - as of version 5.1 * {{ll|Extension:Scribunto/Lua reference manual#Patterns|2=Scribunto patterns}} * {{ll|Extension:Scribunto/Lua reference manual#Ustring patterns|2=Scribunto Unicode string patterns}} == <code>pos</code> == This function returns a single character from the target string at position pos. Usage: : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}pos{{!}}''$1''{{!}}''$2''<nowiki>}}</nowiki></code> |1=target_string |2=index_value }} OR : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}pos{{!}}target= ''$1'' {{!}}pos= ''$2'' <nowiki>}}</nowiki></code> |1=target_string |2=index_value }} Parameters: ; <code>target</code> : The string to search ; <code>pos</code> : The index for the character to return The first character has an index value of 1. If one requests a negative value, this function will select a character by counting backwards from the end of the string. In other words <code>pos = -1</code> is the same as asking for the last character. A requested value of zero, or a value greater than the length of the string returns an error. == <code>find</code> == This function allows one to search for a target string or pattern within another string. Usage: : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}find{{!}}''$1''{{!}}''$2''{{!}}''$3''{{!}}''$4''<nowiki>}}</nowiki></code> |1=source_str |2=target_string |3=start_index |4=plain_flag }} OR : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}find{{!}}source= ''$1'' {{!}}target= ''$2'' {{!}}start= ''$3'' {{!}}plain= ''$4'' <nowiki>}}</nowiki></code> |1=source_str |2=target_str |3=start_index |4=plain_flag }} Parameters: ; <code>source</code> : The string to search ; <code>target</code> : The string or pattern to find within source ; <code>start</code> : The index within the source string to start the search, defaults to 1 ; <code>plain</code> : Boolean flag indicating that target should be understood as plain text and not as a [[w:Lua (programming language)|Lua]]-style [[w:Regular expression|regular expression]], defaults to {{phpi|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. This function should be safe for UTF-8 strings. == <code>replace</code> == This function allows one to replace a target string or pattern within another string. Usage: : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}replace{{!}}''$1''{{!}}''$2''{{!}}''$3''{{!}}''$4''{{!}}''$5''<nowiki>}}</nowiki></code> |1=source_str |2=pattern_string |3=replace_string |4=replacement_count |5=plain_flag }} OR : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}replace{{!}}source= ''$1'' {{!}}pattern= ''$2'' {{!}}replace= ''$3'' {{!}}count= ''$4'' {{!}}plain= ''$5'' <nowiki>}}</nowiki></code> |1=source_string |2=pattern_string |3=replace_string |4=replacement_count |5=plain_flag }} Parameters: ; <code>source</code> : The string to search ; <code>pattern</code> : The string or pattern to find within source ; <code>replace</code> : The replacement text ; <code>count</code> : The number of occurrences to replace; defaults to all ; <code>plain</code> : Boolean flag indicating that pattern should be understood as plain text and not as a [[Special:MyLanguage/Extension:Scribunto/Lua reference manual#Ustring patterns|Scribunto ustring pattern]] (a unicode-friendly [[w:Lua (programming language)|Lua]]-style [[w:Regular expression|regular expression]]); defaults to {{phpi|true}} == <code>rep</code> == Repeats a string ''n'' times. Usage: : {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}rep{{!}}''$1''{{!}}''$2''<nowiki>}}</nowiki></code> |1=source |2=count }} Parameters: ; <code>source</code> : The string to repeat ; <code>count</code> : The number of repetitions. Example {{tmpl|0=<code><nowiki>{{#invoke:</nowiki>String{{!}}rep{{!}}$1{{!}}3<nowiki>}}</nowiki></code>|hello}} gives {{tmpl|0={{#invoke:String|rep|$1|3}}|hello}} <includeonly>{{Sandbox other|| <!-- Categories below this line; interwikis at Wikidata --> [[Category:Modules]] }}</includeonly><noinclude> [[Category:Module documentation pages{{#translation:}}]] </noinclude> | #default= {{#invoke:Template translation|renderTranslatedTemplate|template=Module:String/doc|noshift=1|uselang={{int:lang}}}} }}
Summary:
Please note that all contributions to pub.solar wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
pub.solar wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Dir
(
edit
)
Template:Documentation subpage
(
edit
)
Template:Documentation subpage/en
(
edit
)
Template:Flatlist
(
edit
)
Template:Flatlist/styles.css
(
edit
)
Template:Inline-code
(
edit
)
Template:IsDocSubpage
(
edit
)
Template:Ll
(
edit
)
Template:Localized link
(
edit
)
Template:Magic words
(
edit
)
Template:Mbox
(
edit
)
Template:Module other
(
edit
)
Template:Module rating
(
edit
)
Template:Nmbox
(
edit
)
Template:Nmbox/styles.css
(
edit
)
Template:Ombox
(
edit
)
Template:Pagelang
(
edit
)
Template:Phpi
(
edit
)
Template:Tmpl
(
edit
)
Template:Translatable
(
edit
)
Template:Yesno
(
edit
)
Module:Arguments
(
edit
)
Module:Message box
(
edit
)
Module:Message box/configuration
(
edit
)
Module:Message box/ombox.css
(
edit
)
Module:String
(
edit
)
Module:Template translation
(
edit
)
Module:Tmpl
(
edit
)
Module:Yesno
(
edit
)