Choose where you’d like to start

replaceFirst()

Overview

The replaceFirst() function takes string, searchString, and replacementString as arguments.

It replaces the first occurrence of searchString with the replacementString in the string, and returns the string.

If searchString is not present in the string, it will return the string as it is.

Note: This function performs a case-sensitive search.

Return Type

  • Text

Syntax

<variable> = <string>.replaceFirst( <searchString>, <replacementString>, <boolean>);

(OR)

<variable> = replaceFirst( <string>, <searchString>, <replacementString>, <boolean>);
ParameterDescriptionData type
<variable>Variable which will contain the returned string.TEXT
<string>The string in which the first occurrence of searchString will be replaced with replacementString.TEXT
<searchString>

The first occurrence of this searchString will be replaced with replacementString, in the string.

Note: searchString can also be in the form of regular expression.
TEXT
<replacementString>This replacementString will replace the first occurrence of searchString in the string.TEXT
<boolean>

Applicable values: true or false.

By default, the replaceFirst() function supports regular expression (i.e) it will not find and replace the special characters like $ in the source string.

This parameter can be used to specify if the regular expression is to be supported or escaped.

  • The value true denotes escaping the regular expression.
  • The value false denotes supporting the regular expression. By default, the value is taken as "false".
BOOLEAN

Examples

mainString="Create online database applications";
newText = replaceFirst(mainString, "online", "custom");   // returns Create custom database applications

useremail = "nancy@zylker.com";
newdomain_email = replaceFirst(useremail,"\\@[a-z]{6}\\.[a-z]{3}","@zylkertech.com");
//returns nancy@zylkertech.com

Get Started Now

Execute