J u m p t o c o n t e n t
M a i n m e n u
M a i n m e n u
N a v i g a t i o n
● M a i n p a g e
● C o n t e n t s
● C u r r e n t e v e n t s
● R a n d o m a r t i c l e
● A b o u t W i k i p e d i a
● C o n t a c t u s
● D o n a t e
C o n t r i b u t e
● H e l p
● L e a r n t o e d i t
● C o m m u n i t y p o r t a l
● R e c e n t c h a n g e s
● U p l o a d f i l e
S e a r c h
Search
A p p e a r a n c e
● C r e a t e a c c o u n t
● L o g i n
P e r s o n a l t o o l s
● C r e a t e a c c o u n t
● L o g i n
P a g e s f o r l o g g e d o u t e d i t o r s l e a r n m o r e
● C o n t r i b u t i o n s
● T a l k
( T o p )
1
U s a g e
2
P a r a m e t e r s
T o g g l e P a r a m e t e r s s u b s e c t i o n
2 . 1
D e t a i l s
2 . 2
E q u a l s i g n s a n d p i p e s
2 . 3
W h i t e s p a c e
3
E x a m p l e s
4
S e e a l s o
T o g g l e t h e t a b l e o f c o n t e n t s
T e m p l a t e : S t r r e p : D i f f e r e n c e b e t w e e n r e v i s i o n s
8 7 l a n g u a g e s
● A f r i k a a n s
● А л т а й т и л
● ا ل ع ر ب ي ة
● A s t u r i a n u
● A z ə r b a y c a n c a
● ت ۆ ر ک ج ه
● B a s a B a l i
● ব া ং ল া
● 閩 南 語 / B â n - l â m - g ú
● Б а ш ҡ о р т с а
● Б е л а р у с к а я
● Б е л а р у с к а я ( т а р а ш к е в і ц а )
● भ ो ज प ु र ी
● B i k o l C e n t r a l
● B o s a n s k i
● Б у р я а д
● C a t a l à
● Ч ӑ в а ш л а
● D a n s k
● D a v v i s á m e g i e l l a
● E e s t i
● Э р з я н ь
● E u s k a r a
● ف ا ر س ی
● F ø r o y s k t
● G a l e g o
● گ ی ل ک ی
● 客 家 語 / H a k - k â - n g î
● 한 국 어
● H a u s a
● Հ ա յ ե ր ե ն
● H r v a t s k i
● I l o k a n o
● И р о н
● J a w a
● К о м и
● K u r d î
● К ы р г ы з ч а
● Л а к к у
● L a t v i e š u
● Л е з г и
● म ै थ ि ल ी
● М а к е д о н с к и
● മ ല യ ാ ള ം
● म र ा ठ ी
● ဘ ာ သ ာ မ န ်
● م ا ز ِ ر و ن ی
● B a h a s a M e l a y u
● 閩 東 語 / M ì n g - d ĕ ̤ n g - n g ṳ ̄
● М о к ш е н ь
● မ ြ န ် မ ာ ဘ ာ သ ာ
● 日 本 語
● Н о х ч и й н
● ଓ ଡ ଼ ି ଆ
● O ʻ z b e k c h a / ў з б е к ч а
● ਪ ੰ ਜ ਾ ਬ ੀ
● پ ښ ت و
● ភ ា ស ា ខ ្ ម ែ រ
● P o r t u g u ê s
● R o m â n ă
● Р у с и н ь с к ы й
● Р у с с к и й
● S c o t s
● ස ි ං හ ල
● S i m p l e E n g l i s h
● S l o v e n š č i n a
● ک و ر د ی
● С р п с к и / s r p s k i
● S u o m i
● S v e n s k a
● T a g a l o g
● T a q b a y l i t
● Т а т а р ч а / t a t a r ç a
● ၽ ႃ ႇ သ ႃ ႇ တ ႆ း
● త ె ల ు గ ు
● ไ ท ย
● T h u ɔ ŋ j ä ŋ
● Т о ҷ и к ӣ
● ತ ು ಳ ು
● T ü r k ç e
● У д м у р т
● У к р а ї н с ь к а
● ا ر د و
● V è n e t o
● T i ế n g V i ệ t
● 粵 語
● 中 文
E d i t l i n k s
● T e m p l a t e
● T a l k
E n g l i s h
● R e a d
● V i e w s o u r c e
● V i e w h i s t o r y
T o o l s
T o o l s
A c t i o n s
● R e a d
● V i e w s o u r c e
● V i e w h i s t o r y
G e n e r a l
● W h a t l i n k s h e r e
● R e l a t e d c h a n g e s
● U p l o a d f i l e
● S p e c i a l p a g e s
● P e r m a n e n t l i n k
● P a g e i n f o r m a t i o n
● G e t s h o r t e n e d U R L
● D o w n l o a d Q R c o d e
● W i k i d a t a i t e m
P r i n t / e x p o r t
● D o w n l o a d a s P D F
● P r i n t a b l e v e r s i o n
I n o t h e r p r o j e c t s
● W i k i m e d i a C o m m o n s
● W i k i s p e c i e s
● W i k i q u o t e
● W i k i v e r s i t y
A p p e a r a n c e
H e l p
F r o m W i k i p e d i a , t h e f r e e e n c y c l o p e d i a
B r o w s e h i s t o r y i n t e r a c t i v e l y
← P r e v i o u s e d i t
Content deleted Content added
(5 intermediate revisions by 5 users not shown)
Line 1:
Line 1:
{{#invoke:String|replace|source={{{1}}}|{{{2}}}|{{{3}}}|1}}<noinclude>
{{{{{|safesubst:}}}str left|{{{1}}}|{{{{{|safesubst:}}}#expr: {{{{{|safesubst:}}}str find|{{{1}}}|{{{2}}}}} - 1}}}}{{{3}}}{{{{{|safesubst:}}}str right|{{{1}}}|{{{{{|safesubst:}}}#expr: {{{{{|safesubst:}}}str find|{{{1}}}|{{{2}}}}} + {{{{{|safesubst:}}}str len|{{{2}}}}} - 1}}}}<noinclude>
{{Documentation}}
{{Documentation}}</noinclude>
</noinclude>
L a t e s t r e v i s i o n a s o f 0 4 : 5 9 , 1 7 F e b r u a r y 2 0 2 4
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.
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)
t
e
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)
Select/remove specific item from string
{{ 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
Magic words : About {{padleft:|}}
, {{lc:|}}
, {{formatnum:|}}
etc.
ParserFunctions : About {{#titleparts:|}}, {{#expr:|}}, {{#ifeq:|}},
etc.
Category:String manipulation templates
Category:String manipulating modules
R e t r i e v e d f r o m " https://en.wikipedia.org/w/index.php?title=Template:Str_rep&oldid=1208350174 "
C a t e g o r i e s :
● T e m p l a t e s b a s e d o n t h e S t r i n g L u a m o d u l e
● S t r i n g m a n i p u l a t i o n t e m p l a t e s
H i d d e n c a t e g o r y :
● W i k i p e d i a f u l l y p r o t e c t e d t e m p l a t e s
● T h i s p a g e w a s l a s t e d i t e d o n 1 7 F e b r u a r y 2 0 2 4 , a t 0 4 : 5 9 ( U T C ) .
● T e x t i s a v a i l a b l e u n d e r t h e C r e a t i v e C o m m o n s A t t r i b u t i o n - S h a r e A l i k e L i c e n s e 4 . 0 ;
a d d i t i o n a l t e r m s m a y a p p l y . B y u s i n g t h i s s i t e , y o u a g r e e t o t h e T e r m s o f U s e a n d P r i v a c y P o l i c y . W i k i p e d i a ® i s a r e g i s t e r e d t r a d e m a r k o f t h e W i k i m e d i a F o u n d a t i o n , I n c . , a n o n - p r o f i t o r g a n i z a t i o n .
● P r i v a c y p o l i c y
● A b o u t W i k i p e d i a
● D i s c l a i m e r s
● C o n t a c t W i k i p e d i a
● C o d e o f C o n d u c t
● D e v e l o p e r s
● S t a t i s t i c s
● C o o k i e s t a t e m e n t
● M o b i l e v i e w