Jump to content
Wikipedia The Free Encyclopedia

Help:Manipulating strings

From Wikipedia, the free encyclopedia
This help page is a how-to guide.
It explains concepts or processes used by the Wikipedia community. It is not one of Wikipedia's policies or guidelines and may reflect varying levels of consensus.

The English Wikipedia has several templates and Lua modules which can format or manipulate strings. In this context a string is any piece of text forming part of a page. This help page covers a few useful techniques; look in the navbox below for the full catalogue of templates.

Substrings

[edit ]

The simplest operation is taking a substring, a snippet of the string taken at a certain offset (called an "index") from the start or end. There are a number of legacy templates offering this (see navbox) but for new code use {{#invoke:String |sub|string|startIndex|endIndex}}. The indices are one-based (meaning the first is number one), inclusive (meaning the indices you specify are included), and may be negative to count from the other end. For example, {{#invoke:string|sub|12345678|2|-3}}23456. Not all the legacy substring templates use this numbering scheme, so check the documentation of unfamiliar templates.

Using existing templates

[edit ]

If you think that someone will have done what you want before, look in the navbox below and check. It is much easier to find and use an existing template than to write complex code to do it all in one place.

Look for a template that will do what you want all in one go. For example, rather than taking the final six characters of a string and checking if they are equal to "navbox", use {{str endswith |string|navbox}}.

Automatically trimmed whitespace

[edit ]

If you pass the string " abc def " (without quotes) to a template via a named or explicitly numbered parameter (like {{template|1= abc def }} the spaces on the outside will be trimmed off and will not be counted for anything the template does with that parameter. It will see the string abc def.

If you use automatically numbered parameters ({{template| abc def }}) the spaces on the outside do count, but some templates may still choose to remove them themselves.

Lua patterns (regex)

[edit ]

Regular expressions (or regex) are a common and very versatile programming technique for manipulating strings. On Wikipedia you can use a limited version of regex called a Lua pattern to select and modify bits of text from a string. The pattern is a piece of code describing what you are looking for in the string. The symbols you an use in a pattern are:

  • . means any individual character. ... would mean any three characters, etc.
  • *, +, ?, and - are the quantifiers. They mean that the previous character can be repeated n times, where for each symbol n ≥ 0, n > 0, n is zero or one, and n ≥ 0 again respectively. (The difference with - is that it is "non-greedy", it matches as few symbols as possible given the rest of the pattern.)
  • ^ means the start of the string, and $ means the end.
  • [abc] means any symbol out of a, b or c, and [^abc] means anything that isn't a, b or c.
  • Preceding any of the above with a % takes away their normal meaning and makes them mean "literally" the symbol they are. Preceding anything else with a % (like %a) has a special meaning which you can check in the manual.

Putting this all together, ^[Aa]*b?c matches the first six characters of "AaAabcccc".

By wrapping part of the pattern in brackets, you can extract it, referencing it with the code %1. Example:

  • The find-replace instruction {{#invoke:string|replace|AaAabc XYZ|^([Aa]*)b?c|%1|plain=false}} gives AaAa XYZ
  • We can discard the XYZ by putting .* at the end of the search string; this picks up anything after the rest of the pattern. {{#invoke:string|replace|AaAabc XYZ|^([Aa]*)b?c.*|%1|plain=false}} gives AaAa.

StringFunctions (from ParserFunctions)

[edit ]

Wikipedia does not have the "StringFunctions" series of parser functions (listed below), and is not going to get them (per phab:T8455). Instead, templates use Lua (via Module:String or otherwise), alongside existing parser functions.

None of these functions will work, but they have alternatives:

Testing code

[edit ]

If you're not sure what some code is going to do, paste it into Special:ExpandTemplates, which will evaluate it for you to view.

See also

[edit ]

See navbox chart below for various string-handling templates or parser functions.

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).
{{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.
{{WikiProjectbasename }} Removes "WikiProject" 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.
{{str count }} Count the number of occurrences of a pattern within a string.
{{string count in page }} Count the number of occurrences of a string within a given page's wikitext.
{{Array }} Array item manipulation and retrieval.
{{a or an }} Takes a noun and produces the appropriate indefinite article, based on whether the first letter is a vowel.
{{in string }} Returns optional string when given string is not found
{{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
Retrieved from "https://en.wikipedia.org/w/index.php?title=Help:Manipulating_strings&oldid=1302839013"

AltStyle によって変換されたページ (->オリジナル) /