Chức năng thay thế VBScript
❮ Toàn bộ tài liệu tham khảo VBScript
Hàm Replace sẽ thay thế một phần được chỉ định của một chuỗi bằng một chuỗi khác với số lần được chỉ định.
Cú pháp
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:
|
Các ví dụ
ví dụ 1
Thay thế từ "đẹp" bằng "tuyệt vời":
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"beautiful","fantastic"))
%>
Đầu ra của đoạn mã trên sẽ là:
This is a fantastic day!
Ví dụ 2
Thay thế ký tự "i" bằng "##":
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##"))
%>
Đầu ra của đoạn mã trên sẽ là:
Th##s ##s a beaut##ful day!
Ví dụ 3
Thay thế ký tự "i" bằng "##", bắt đầu từ vị trí 15:
Lưu ý rằng tất cả các ký tự trước vị trí 15 đều bị xóa.
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",15))
%>
Đầu ra của đoạn mã trên sẽ là:
t##ful day!
Ví dụ 4
Thay thế 2 lần xuất hiện đầu tiên của chữ "i" bằng "##", bắt đầu từ vị trí 1:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",1,2))
%>
Đầu ra của đoạn mã trên sẽ là:
Th##s ##s a beautiful day!
Ví dụ 5
Thay thế ký tự "t" bằng "##", bằng văn bản và nhị phân, so sánh:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"t","##",1,-1,1) & "<br />")
response.write(Replace(txt,"t","##",1,-1,0))
%>
Đầu ra của đoạn mã trên sẽ là:
##his is a beau##iful day!
This is a beau##iful day!
❮ Toàn bộ tài liệu tham khảo VBScript