وظيفة استبدال VBScript
❮ استكمال مرجع VBScript
تستبدل الدالة Replace جزءًا محددًا من سلسلة بسلسلة أخرى بعدد محدد من المرات.
بناء الجملة
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:
|
أمثلة
مثال 1
استبدل كلمة "جميلة" بكلمة "رائعة":
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"beautiful","fantastic"))
%>
ناتج الكود أعلاه سيكون:
This is a fantastic day!
مثال 2
استبدل الحرف "i" بـ "##":
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##"))
%>
ناتج الكود أعلاه سيكون:
Th##s ##s a beaut##ful day!
مثال 3
استبدل الحرف "i" بـ "##" ، بدءًا من الموضع 15:
لاحظ أنه تم حذف جميع الأحرف قبل الموضع 15.
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",15))
%>
ناتج الكود أعلاه سيكون:
t##ful day!
مثال 4
استبدل أول ظهورين للحرف "i" بـ "##" ، بدءًا من الموضع 1:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"i","##",1,2))
%>
ناتج الكود أعلاه سيكون:
Th##s ##s a beautiful day!
مثال 5
استبدل الحرف "t" بـ "##" ، بالمقارنة النصية والثنائية:
<%
txt="This is a beautiful day!"
response.write(Replace(txt,"t","##",1,-1,1) & "<br />")
response.write(Replace(txt,"t","##",1,-1,0))
%>
ناتج الكود أعلاه سيكون:
##his is a beau##iful day!
This is a beau##iful day!
❮ استكمال مرجع VBScript