A function to replace characters in a string

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

A function to replace characters in a string

Postby arjfca » 30 Dec 2015

//This function called "ReplaceStr" replace characters in a string
// By Martin Theriault Dec 29 2015


Input,
String to be modified,
Characters to be replace,
Replacement Characters

ReplaceStr("Eur.Usd",".","_") = "Eur_Usd"

ReplaceStr("Hello my name is Martin", "my", "your") = "Hello your name is Martin"

ReplaceStr("Hello my name is Martin", "Martin", "Paul") + "Hello my name is Paul"

Code: Select all

//ReplaceSTR will replace a series of characters with the new one
Input:
InputString(String),
Lookfor (String),
ReplaceBy (String);

Var:

Position(-1),
LengthInput (0),
Length_ReplaceBy (0),
Length_LookFor (0),
Temp_Length (0),
TempString ("");


LengthInput = strlen(InputString);
Length_Lookfor = strlen(Lookfor);
Length_ReplaceBy = StrLen(ReplaceBy);

Position = Instr(InputString,LookFor);
TempString =LeftStr(inputString,(position-1 ))+ replaceby;
Temp_Length = StrLen(TempString);
TempString = tempstring + RightStr(InputString, (LengthInput -(position + length_lookfor -1)));

replacestr = Tempstring;
print ("exit>: , ", replacestr);

Return to “User Contributed Studies and Indicator Library”