Funkce nahrazení jazyka VBScript


❮ Kompletní VBScript Reference

Funkce Nahradit nahradí zadanou část řetězce jiným řetězcem určeným počtem opakování.

Syntax

Replace(string,find,replacewith[,start[,count[,compare]]])

Parameter Description
string Required. The string to be searched
find Required. The part of the string that will be replaced
replacewith Required. The replacement substring
start Optional. Specifies the start position. Default is 1. All characters before the start position will be removed.
count Optional. Specifies the number of substitutions to perform.
Default value is -1, which means make all possible substitutions
compare Optional. Specifies the string comparison to use. Default is 0

Can have one of the following values:

  • 0 = vbBinaryCompare - Perform a binary comparison
  • 1 = vbTextCompare - Perform a textual comparison

Příklady

Příklad 1

Nahraďte slovo „krásný“ slovem „fantastický“:

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"beautiful","fantastic"))

%>

Výstupem výše uvedeného kódu bude:

This is a fantastic day!

Příklad 2

Nahraďte písmeno "i" za "##":

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"i","##"))

%>

Výstupem výše uvedeného kódu bude:

Th##s ##s a beaut##ful day!

Příklad 3

Nahraďte písmeno „i“ za „##“, počínaje pozicí 15:

Všimněte si, že všechny znaky před pozicí 15 jsou odstraněny.

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",15))

%>

Výstupem výše uvedeného kódu bude:

t##ful day!

Příklad 4

Nahraďte 2 první výskyty písmene „i“ za „##“, počínaje pozicí 1:

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",1,2))

%>

Výstupem výše uvedeného kódu bude:

Th##s ##s a beautiful day!

Příklad 5

Nahraďte písmeno "t" za "##" s textovým a binárním srovnáním:

<%

txt="This is a beautiful day!"
response.write(Replace(txt,"t","##",1,-1,1) & "<br />")
response.write(Replace(txt,"t","##",1,-1,0))

%>

Výstupem výše uvedeného kódu bude:

##his is a beau##iful day!
This is a beau##iful day!

❮ Kompletní VBScript Reference