Funzione di sostituzione VBScript
❮ Riferimento completo di VBScript
La funzione Sostituisci sostituisce una parte specificata di una stringa con un'altra stringa un numero di volte specificato.
Sintassi
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:
|
Esempi
Esempio 1
Sostituisci la parola "bello" con "fantastico":
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"beautiful","fantastic"))
%>
L'output del codice sopra sarà:
This is a fantastic day!
Esempio 2
Sostituisci la lettera "i" con "##":
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##"))
%>
L'output del codice sopra sarà:
Th##s ##s a beaut##ful day!
Esempio 3
Sostituisci la lettera "i" con "##", a partire dalla posizione 15:
Nota che tutti i caratteri prima della posizione 15 vengono rimossi.
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",15))
%>
L'output del codice sopra sarà:
t##ful day!
Esempio 4
Sostituisci le 2 prime occorrenze della lettera "i" con "##", a partire dalla posizione 1:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",1,2))
%>
L'output del codice sopra sarà:
Th##s ##s a beautiful day!
Esempio 5
Sostituisci la lettera "t" con "##", con confronto testuale e binario:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"t","##",1,-1,1) & "<br />")
response.write(Replace(txt,"t","##",1,-1,0))
%>
L'output del codice sopra sarà:
##his is a beau##iful day!
This is a beau##iful day!
❮ Riferimento completo di VBScript