Content deleted Content added
→Examples: Stringsplit template |
→Stringsplit: more examples |
||
Line 182:
[[Template:Stringsplit]] is a convenience wrapper for the split function.
* <code><nowiki>{{Stringsplit |This is a piece of text to be split |" "}}</nowiki></code> → {{Stringsplit |This is a piece of text to be split |" "}}
* <code><nowiki>{{Stringsplit |This is a piece of text to be split |" "| 4}}</nowiki></code> → {{Stringsplit |This is a piece of text to be split |" "| 4}}
* <code><nowiki>{{Stringsplit |This is a piece of text to be split |x| 2}}</nowiki></code> → {{Stringsplit |This is a piece of text to be split |x| 2}}
Modules may return strings with | as separators like this: <code><nowiki>{{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }}</nowiki></code> → {{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }}
* <code><nowiki>{{Stringsplit |{{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }}|{{!}}| 2}}</nowiki></code> → {{Stringsplit |{{#invoke:carousel | main | name = WPDogs | switchsecs = 5 }}|{{!}}| 2}}
Lua patterns can allow splitting at classes of characters such as punctuation:
* <code><nowiki>{{Stringsplit |Apples, pears, oranges; Cats, dogs|"%p"| 2 |false}}</nowiki></code> → {{Stringsplit |Apples, pears, oranges; Cats, dogs|"%p"| 2 |false}}
* <code><nowiki>{{Stringsplit |Apples, pears, oranges; Cats, dogs|"%p"| 4 |false}}</nowiki></code> → {{Stringsplit |Apples, pears, oranges; Cats, dogs|"%p"| 4 |false}}
Or split on anything that isn't a letter (no is treated as false):
* <code><nowiki>{{Stringsplit |Apples pears oranges; Cats dogs|"%A+"| 4 |no}}</nowiki></code> → {{Stringsplit |Apples pears oranges; Cats dogs|"%A+"| 4 |no}}
Named parameters force the trimming of leading and trailing spaces in the parameters and are generally clearer when used:
* <code><nowiki>{{Stringsplit | txt=Apples pears oranges; Cats dogs | sep="%A+" | idx=3 | plain=false }}</nowiki></code> → {{Stringsplit | txt=Apples pears oranges; Cats dogs | sep="%A+" | idx=3 | plain=false }}
== See also ==
|