Funkcja zastępowania VBScript
❮ Pełne odniesienie do VBScript
Funkcja Replace zastępuje określoną część ciągu innym ciągiem określoną liczbę razy.
Składnia
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:
|
Przykłady
Przykład 1
Zamień słowo „piękny” na „fantastyczny”:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"beautiful","fantastic"))
%>
Wynikiem powyższego kodu będzie:
This is a fantastic day!
Przykład 2
Zamień literę „i” na „##”:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##"))
%>
Wynikiem powyższego kodu będzie:
Th##s ##s a beaut##ful day!
Przykład 3
Zamień literę „i” na „##”, zaczynając od pozycji 15:
Zauważ, że wszystkie znaki przed pozycją 15 są usuwane.
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",15))
%>
Wynikiem powyższego kodu będzie:
t##ful day!
Przykład 4
Zamień 2 pierwsze wystąpienia litery „i” na „##”, zaczynając od pozycji 1:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",1,2))
%>
Wynikiem powyższego kodu będzie:
Th##s ##s a beautiful day!
Przykład 5
Zamień literę „t” na „##” na porównanie tekstowe i binarne:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"t","##",1,-1,1) & "<br />")
response.write(Replace(txt,"t","##",1,-1,0))
%>
Wynikiem powyższego kodu będzie:
##his is a beau##iful day!
This is a beau##iful day!
❮ Pełne odniesienie do VBScript