Função de substituição do VBScript
❮ Referência completa do VBScript
A função Replace substitui uma parte especificada de uma string por outra string um número especificado de vezes.
Sintaxe
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:
|
Exemplos
Exemplo 1
Substitua a palavra "bonito" por "fantástico":
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"beautiful","fantastic"))
%>
A saída do código acima será:
This is a fantastic day!
Exemplo 2
Substitua a letra "i" por "##":
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##"))
%>
A saída do código acima será:
Th##s ##s a beaut##ful day!
Exemplo 3
Substitua a letra "i" por "##", começando na posição 15:
Observe que todos os caracteres antes da posição 15 são removidos.
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",15))
%>
A saída do código acima será:
t##ful day!
Exemplo 4
Substitua as 2 primeiras ocorrências da letra "i" por "##", começando na posição 1:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",1,2))
%>
A saída do código acima será:
Th##s ##s a beautiful day!
Exemplo 5
Substitua a letra "t" por "##", com comparação textual e binária:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"t","##",1,-1,1) & "<br />")
response.write(Replace(txt,"t","##",1,-1,0))
%>
A saída do código acima será:
##his is a beau##iful day!
This is a beau##iful day!
❮ Referência completa do VBScript