Template:String split/doc

Template:String split is a convenience wrapper for the split function in Module:String2.

The split function splits text at boundaries specified by separator and returns the chunk for the index idx (starting at 1). It can use positional parameters or named parameters (but these should not be mixed):

  • {{#invoke:String2 |split |text |separator |index |true/false}}
  • {{#invoke:String2 |split |txt=text |sep=separator |idx=index |plain=true/false}}

Any double quotes (") in the separator parameter are stripped out, which allows spaces and wikitext like ["[ to be passed. Use {{!}} for the pipe character |.

If the optional plain parameter is set to false / no / 0 then separator is treated as a Lua pattern. The default is plain=true, i.e. normal text matching.

The index parameter is optional; it defaults to the first chunk of text. A negative parameter value counts chunks backward from the end of the text.

Examples

  • {{String split |This is a piece of text to be split |" "}} → This
  • {{String split |This is a piece of text to be split |" "| 4}} → piece
  • {{String split |This is a piece of text to be split |x| 2}} → t to be split

Modules may return strings with | as separators like this: {{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }} → YellowLabradorLooking new.jpg | Yellow Labrador Retriever

  • {{String split |{{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }}|{{!}}| 2}} → Yellow Labrador Retriever

Lua patterns can allow splitting at classes of characters such as punctuation:

  • {{String split |Apples, pears, oranges; Cats, dogs|"%p"| 2 |false}} → pears
  • {{String split |Apples, pears, oranges; Cats, dogs|"%p"| 4 |false}} → Cats

Or split on anything that isn't a letter (no is treated as false):

  • {{String split |Apples pears oranges; Cats dogs|"%A+"| 4 |no}} → Cats

Named parameters force the trimming of leading and trailing spaces in the parameters and are generally clearer when used:

  • {{String split | txt=Apples pears oranges; Cats dogs | sep="%A+" | idx=3 | plain=false }} → oranges

A negative index will count back from the end of the text:

  • {{String split |txt=This is a piece of text to be split |sep=" " |idx=-1}} → split

An easy mistake to make when using negative parameters is to forget that a trailing space in the text when passed as an unnamed parameter will induce an empty last chunk:

  • {{String split |This is a piece of text to be split |" "|-1}}
  • {{String split |This is a piece of text to be split|" "|-1}} → split

See also


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.