Přeskočit na obsah

Modul:String/Dokumentace

Z Wikipedie, otevřené encyklopedie

Převzato a částečně přeloženo z anglické Wikipedie, dokumentaci vizte na en:Module:String.


Tento modul poskytuje přístup k základním operacím s řetězci.


Většinu zde uvedených funkcí lze zadat buď  s pojmenovanými parametry, s nepojmenovanými parametry, nebo kombinace obojího. Pokud jsou použity pojmenované parametry, Mediawiki automaticky odstraní počáteční či koncové mezery. 

Globální možnosti

  • ignore_errors: Pokud je nastavena na 'true' nebo 1, nebude vypsaná chybová zpráva
  • error_category: Pokud dojde k chybě, bude stránka kategorizována. Výchozí kategorie je Kategorie:Chyby hlášené Modulem String.
  • no_category: Pokud je nastavena na 'true' nebo 1, kategorie nebude při chybě přidána.

len

Funkce vrací délku řetězce.

Použití:

{{#invoke:String|len|cílový řetězec}}

NEBO

{{#invoke:String|len|s= cílový řetězec }}

Parametry:

s
řetězec, jehož délku zjišťujeme

sub

Vrací část řetězce podle zadaných parametrů.

Použití:

{{#invoke:String|sub|cílový řetězec|začátek|konec}}

NEBO

{{#invoke:String|sub|s= cílový řetězec |i= začátek |j= konec }}

Parametry:

s
řetězec
i
pozice začátku, výchozí 1.
j
pozice konce, výchozí poslední znak.

První znak řetězce je na pozici 1. Pokud je i NEBO j záporné, počítá se od konce řetězce. -1 je poslední znak.

Pokud jsou zadané pozice mimo rozsah, je hlášena chyba

match

Funkce vrací část řetězce odpovídající zadané šabloně.

Použití:

{{#invoke:String|match|source_string|pattern_string|začátek|match_number|plain_flag|nomatch_output}}

NEBO

{{#invoke:String|match|s= source_string |pattern= pattern_string |start= začátek|match= match_number|plain= plain_flag |nomatch= nomatch_output }}

Parametry:

s
vstupní řetězec
pattern
šablona nebo řetězec pro hledání
start
Index, od jaké pozice hledat. Výchozí je 1
match
V některých případech vyhoví více míst v řetězci. Tento parametr specifikuje, který z nich vrátit, první je číslo 1. Pokud je číslo záporné, je ubažován výskyt od konce. Výchozí je 1.
plain
příznak, který označuje, zda výsledek považovat za čistý text. Výchozí je false
nomatch
poku není řetězec nalezen, vrací tuto hodnotu


Informace a syntaxe Lua, formy regexů:

pos

Funkce vrací znak na zadané pozici řetězce.

Použití:

{{#invoke:String|pos|cílový řetězec|pozice}}

NEBO

{{#invoke:String|pos|target= cílový řetězec |pos= pozice }}

Parametry:

target
vstupní řetězec
pos
pozice hledaného znaku

První znak je na pozici 1.

Z8porná čísla označují pozici od konce řetězce, posledbá znak je na pozici -1.

Pozice 0 nebo větší, než délka řetězce, vrací chybu.

str_find

This function duplicates the behaviNEBO of {{str_find}}, including all of its quirks. This is provided in NEBOder to suppNEBOt existing templates, but is NOT RECOMMENDED fNEBO new code and templates. New code is recommended to use the "find" function instead.

Returns the first index in "source" that is a match to "target". Indexing is 1-based, and the function returns -1 if the "target" string is not present in "source".

ImpNEBOtant Note: If the "target" string is empty / missing, this function returns a value of "1", which is generally unexpected behaviNEBO, and must be accounted fNEBO separatetly.

find

This function allows one to search fNEBO a target string NEBO pattern within another string.

Použití:

{{#invoke:String|find|source_str|cílový řetězec|začátek|plain_flag}}

NEBO

{{#invoke:String|find|source= source_str |target= target_str |start= začátek|plain= plain_flag }}

Parametry:

source
The string to search
target
The string NEBO pattern to find within source
start
The index within the source string to start the search, defaults to 1
plain
Boolean flag indicating that target should be understood as plain text and not as a Lua-style regular expression, defaults to true

This function returns the first index >= "start" where "target" can be found within "source". Indices are 1-based. If "target" is not found, then this function returns 0. If either "source" NEBO "target" are missing / empty, this function also returns 0.

This function should be safe fNEBO UTF-8 strings.

replace

This function allows one to replace a target string NEBO pattern within another string.

Použití:

{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}

NEBO

{{#invoke:String|replace|source= source_string |pattern= pattern_string|replace= replace_string |count= replacement_count |plain= plain_flag }}

Parametry:

source
The string to search
pattern
The string NEBO pattern to find within source
replace
The replacement text
count
The number of occurrences to replace; defaults to all
plain
Boolean flag indicating that pattern should be understood as plain text and not as a Scribunto ustring pattern (a unicode-friendly Lua-style regular expression); defaults to true

rep

Repeats a string n times. A simple function to pipe string.rep to templates. Použití:

{{#invoke:String|rep|source|count}}

Parametry:

source
The string to repeat
count
The number of repetitions.

Example {{#invoke:String|rep|hello|3}} gives hellohellohello