From Wikipedia, the free encyclopedia
{{{1}}}
![Warning](//upload.wikimedia.org/wikipedia/en/thumb/b/b4/Ambox_important.svg/40px-Ambox_important.svg.png) | This template is used in system messages
, and on
approximately 270,000 pages
.
Changes to it can cause immediate changes to the Wikipedia user interface.
To avoid major disruption and server load, any changes should be tested in the template's
/sandbox
or
/testcases
subpages, or in your own
user subpage
. The tested changes can be added to this page in a single edit. Please discuss changes on the
talk page
before implementing them.
|
![](//upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Lua-Logo.svg/30px-Lua-Logo.svg.png) | This template uses
Lua
:
|
Returns a modified copy of a given text, with the
first
occurrence of a find-string replaced with a replacement-string.
Usage
{{Str rep|
text...
|
find-string
|
replace-string
}}
{{Str rep| 1=
text...
| 2=
find-string
| 3=
replace-string
}}
This is equivalent to
{{
#invoke:String
|replace|source=
text...
|
find-string
|
replace-string
|1}}
.
Parameters
There are three positional (unnamed) parameters, all required:
- the initial text string subject to search and replacement
- the find-string to search for in the text
- the replacement string: param 3 replaces param 2's first occurrence in param 1.
Details
The value of the text parameter may be anything that resolves to a string, such as:
- Immediate text entered directly into the parameter field:
{{str rep|Dogs chase cats. Cats chase mice.|...}}
- A parameter passed from a transcluding template:
{{str rep|{{{5|}}}|...}}
or
{{str rep|{{{text|}}}|...}}
- A file or template:
{{str rep|{{Template:Str rep/doc/Dogs and cats}}|...}}
- or anything else that resolves to a string.
Equal signs and pipes
Equal signs in parameter values passed as immediate text either
need to be escaped
as
{{=}}
, or you can leave them unescaped and use the built-in numeric names (
|1=
,
|2=
, and
|3=
) for the three unnamed positional parameters, as in the section
usage
at the top.
Pipe characters in immediate text should be escaped either as
|
or as
{{
!
}}
. See
Help:Template#Hints and workarounds
. Pipe characters in embedded items such as params or template invocations don't need to be escaped.
White space
Pay attention to the use of white space in the parameters; all white space is significant in positional parameters. In particular,
leading and trailing white space
is not removed, and can affect the results. See contrasting
examples #1 and #2
below.
If you like the readability of having convenience blanks before and after the parameters so they're not adjacent to the pipe characters, then use the
built-in numeric names
|1=
, etc. These will not include any leading or trailing blanks in the parameter value; see
example 3
.
Examples
{{str rep|Dogs chase cats.|cats|balls}}
? Dogs chase balls.
{{str rep|Dogs chase cats.|cats |balls }}
? Dogs chase cats. —
8px
N
leading and trailing blanks are significant; see
Details
{{str rep|Dogs chase cats.|2=cats |3=balls }}
? Dogs chase balls. —
Y
... but not when named parameters are used
{{str rep|Dogs chase cats. Cats chase mice.|chase|love}}
? Dogs love cats. Cats chase mice. —
only first occurrence replaced
{{str rep|{{Str rep/doc/Dogs and cats}}|chase|love}}
? Dogs love cats. Cats chase mice. —
text can be entered by a template
See also
- {{
Replace
}}
- to replace
all
occurrences (or a specific number of occurrences)
String-handling templates
(help page)
|
---|
Substring (select chunk of string by its position)
|
---|
{{
str sub new
}}
| Select a substring based on starting and ending index counted in either direction.
| {{
str left
}}
| Keep the first N characters of a string, or duplicate it to N characters.
| {{
str crop
}}
| Return all but the last N characters of a string.
| {{
str index
}}
| Return the N-th character of a string.
|
| {{
str right
}}
| Return substring starting at N-th character to end of string.
| {{
str rightc
}}
| Return the last N characters of a string; or a given value if empty.
| {{
str sub old
}}
| Return substring of given length starting at N-th character.
|
|
|
|
Trimming (select/remove chunk of string by character type)
|
---|
{{
trim
}}
| Trim any leading or trailing whitespace
| {{
str letter/trim
}}
| Trim down to the beginning letters.
| {{
str number/trim
}}
| Trim down to the beginning numbers.
| {{
trim brackets
}}
| Trim any leading or trailing square brackets.
| {{
trim quotes
}}
| Trim leading or trailing matching quotes.
| {{
trim leading
}}
| Trim up to six instances of specified <prefix>.
|
| |
|
|
Select/remove specific item from string
|
---|
{{
delink
}}
,
{{
trim brackets
}}
| Removes wikilink brackets (wikicode);
all
or
outer only
.
| {{
Plain text
}}
| Removes wikicode (formatting and brackets)
| {{
PAGENAMEBASE
}}
| Trim off any parenthesis text at the end of a string (disambiguation).
| {{
title disambig text
}}
| Returns text in parentheses at the end of a string (disambiguation).
| {{
WikiProjectbasename
}}
| Removes "WikiProject" from a pagename.
|
| {{
ship prefix
}}
| Returns the length of a ship's name prefix, if listed (ship names).
| {{
title year
}}
| Returns the 3-or-4-digit year from a pagename, if any.
| {{
title decade
}}
| Returns the 4-digit decade from a pagename (e.g. "1740" from "Foo 1740s bar"), if any.
| {{
title number
}}
| Returns any number from a pagename.
|
|
|
|
Insertion, replacement and length
|
---|
{{
loop
}}
| Repeat character string a specified number of times.
| {{
replace
}}
| Returns the string, after replacing
all
occurrences of a specified string with another string.
| {{
str rep
}}
| Returns the string, after replacing the first occurrence of a specified string with another string.
| {{
digits
}}
| Returns the string, after removing all characters that are not digits 0?9
| {{
Nowiki
}}
| Displays wikitext as plain text.
|
|
{{
str len
}}
| Returns a string's length.
| {{
str ≥ len
}}
| Check if a string is "longer or equally long" or "shorter" than a given length.
| {{
str ≤ len
}}
| Check if a string is "shorter or equally long" or "longer" than a given length.
| {{
str ≠ len
}}
| Check if a string is "not equal" or "equal" to a given length.
|
|
|
|
Analysis and searching
|
---|
{{
str startswith
}}
{{
str endswith
}}
| Check if a string starts or ends with a given string.
| {{
str find
}}
,
{{
strfind short
}}
| Returns the numerical location of a given string in a string.
| {{
in string
}}
| Returns optional string when given string is not found
| {{
str count
}}
| Count the number of occurrences of a pattern within a string.
| {{
reSwitch
}}
| Match one or multiple patterns with a variety of possible outputs.
|
|
{{
a or an
}}
| Takes a noun and produces the appropriate indefinite article, based on whether the first letter is a vowel.
| {{
Array
}}
| Array manipulation and retrieval.
| {{
Isnumeric
}}
| Returns number when true, blank when not numeric.
| {{
str letter
}}
| Returns the number of letters that begin a string.
| {{
str number
}}
| Returns the number of numbers that begin a string.
|
|
|
|
Modules and built-in functions
|
---|
|
|
|