Chức năng thay thế VBScript
❮ Tham khảo VBScript hoàn chỉnh
Hàm Thay thế thay thế một phần được chỉ định của chuỗi bằng một chuỗi khác với số lần xác đị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:
|
Ví dụ
ví dụ 1
Thay 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!
Hiển thị ví dụ »Ví dụ 2
Thay chữ “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!
Hiển thị ví dụ »Ví dụ 3
Thay chữ “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!
Hiển thị ví dụ »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!
Hiển thị ví dụ »Ví dụ 5
Thay thế chữ "t" bằng "##", bằng so sánh văn bản và nhị phân:
<%
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!
Hiển thị ví dụ »❮ Tham khảo VBScript hoàn chỉnh