ColdFusion Replace Function: Uses & How To Implement
As the name implies, ColdFusion Replace function is used to replace a string with another string.
In return you get the specified string replaced with the value you want. In normal condition, the search is case sensitive. This function can take either string or callback function as an argument instead of the argument to replace the string.
ColdFusion Replace Function Syntax
Replace(string, substring1, substring2 [ scope ])
Definition
String: A string in which to search
Substring1: The string or variable you want to change
Substring 2: The string or variable that replaces substring 1
Scope: It defines the occurrence of the replace function in the String.
*one: replaces the first occurrence(default)
*all: replaces all occurrences in the string.
Example:
Consider this sentence: Hi, Our company name is Techversant.
And we are going to change the word ‘Hi’ with ‘Hello’.
The code will be like,
<cfoutput>#replace(“Hi, Our company name is Techversant.”, “Hi”,”Hello”,”one”) #</cfoutput>
The output of the code will be: Hello, Our company name is Techversant.
To remove a string, specify the empty string (“”) as substring2.
Example with callback function
<cfscript>
myStr=”Contact us for ColdFusion software engineering solutions.”;
outStr = replace( myStr, “so”, function (transform, position, original){
return UCase(transform);
}, “all”);
writeoutput(outStr);
</cfscript>
It will return the following output:
Contact us for ColdFusion SOftware engineering SOlutions.
If you have any doubts, please don’t hesitate to contact us. We are always here and happy to help.
ColdFusion Replace Function: Uses & How To Implement
As the name implies, ColdFusion Replace function is used to replace a string with another string.
In return you get the specified string replaced with the value you want. In normal condition, the search is case sensitive. This function can take either string or callback function as an argument instead of the argument to replace the string.
ColdFusion Replace Function Syntax
Replace(string, substring1, substring2 [ scope ])
Definition
String: A string in which to search
Substring1: The string or variable you want to change
Substring 2: The string or variable that replaces substring 1
Scope: It defines the occurrence of the replace function in the String.
*one: replaces the first occurrence(default)
*all: replaces all occurrences in the string.
Example:
Consider this sentence: Hi, Our company name is Techversant.
And we are going to change the word ‘Hi’ with ‘Hello’.
The code will be like,
<cfoutput>#replace(“Hi, Our company name is Techversant.”, “Hi”,”Hello”,”one”) #</cfoutput>
The output of the code will be: Hello, Our company name is Techversant.
Example with callback function
<cfscript>
myStr=”Contact us for ColdFusion software engineering solutions.”;
outStr = replace( myStr, “so”, function (transform, position, original){
return UCase(transform);
}, “all”);
writeoutput(outStr);
</cfscript>
It will return the following output:
Contact us for ColdFusion SOftware engineering SOlutions.
If you have any doubts, please don’t hesitate to contact us. We are always here and happy to help.
Related Articles
An Overview to ColdFusion Frameworks
ColdFusion for Mobile Application Development