Replace Function

Tips >>ASP Functions

WANTS TO MAKE SOFTWARE WITHOUT KNOWLEDGE OF ASP? CLICK HERE

The Replace function replaces a specified part of a string with another string a specified number of times. In other words, the Replace function can be used to replace certain characters within a string with other characters.It can also be explained as the ASP Replace() (if we want to be correct the Replace() function is a VBScript function) is a handy function, used to replace sub-strings found within a string.

The Syntax of the Replace function is :-

Replace(string,find,replacewith[,start[,count[,compare]]])

The following are the Mandatory Arguments :-

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.
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:·

  • 0 = vbBinaryCompare - Perform a binary comparison.
  • 1 = vbTextCompare - Perform a textual comparison

Example#1 :-

Code :-
dim txt
txt="This is a beautiful day!"
document.write(Replace(txt,"beautiful","horrible"))
Output :-
This is a horrible day!

Example#2 :-

Code :-
<%
sMyString = "Who-was-that?"
sMyString = Replace(sMyString, "-", " ")
Response.Write sMyString
%>
Output :-
Who was that?

Example#3 :-

Code :-
<%
String1="My_Name_is_Michael"
Response.write Replace(String1,"_"," ")
%>
Output :-
My Name is Michael

Example#4 :-
It explains clearly the Replace Substring

Code :-
<%
=Replace("How now brown cow?", "brown", "purple")
%>
Output :-

How now purple cow?

Example#5 :-
It explains clearly the Start Argument

Code :-
<%
=Replace("How now brown cow?", "brown", "purple", 7)
%>
Output :-
w purple cow?

Note :- The portion of the string to the left of the start position will be discarded.

Example#6 :-
It explains clearly the Count Argument

Code :-
<%
=Replace("red blue red-blue redblue bluered", "blue", "purple", 1, 3) %>
Output :-
red purple red-purple redpurple bluered

Example#7 :-
It explains clearly the Compare Argument VBBinaryCompare 0

Code :-
<%
=Replace("red blue red-blue redblue bluered", "BLUE", "PURPLE", 1, 3, 0)
%>
Output :-
red blue red-blue redblue bluered

Example#8 :-
It explains clearly the compare argument VBTextCompare 1

Code :-
<%
=Replace("red blue red-blue redblue bluered", "BLUE", "PURPLE", 1, 3, 1)
%>
Output :-

red PURPLE red-PURPLE redPURPLE bluered

 
E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641