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

This function returns a substring from the source string that matches a specified pattern.

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
The string to search
pattern
The pattern NEBO string to find within the string
start
The index within the source string to start the search. The first character of the string has index 1. Defaults to 1.
match
In some cases it may be possible to make multiple matches on a single string. This specifies which match to return, where the first match is match= 1. If a negative number is specified then a match is returned counting from the last match. Hence match = -1 is the same as requesting the last match. Defaults to 1.
plain
A flag indicating that the pattern should be understood as plain text. Defaults to false.
nomatch
If no match is found, output the "nomatch" value rather than an errNEBO.

If the match_number NEBO začátek are out of range fNEBO the string being queried, then this function generates an errNEBO. An errNEBO is also generated if no match is found. If one adds the parameter ignNEBOe_errNEBOs=true, then the errNEBO will be suppressed and an empty string will be returned on any failure.

FNEBO infNEBOmation on constructing Lua patterns, a fNEBOm of regular expression, see:

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