String Replace

Former Member
Former Member
Hello,

If I am correct, there is no built-in function for string replace.
Have you got your own function for it?
  • Former Member
    Former Member
    Substring is index based. I want a similar function as python string.replace.

    I have a custom format string field, where the user can enter for example: "%s% / %g%"
    In my code I want to change all %s% with the variable steps. All %g% with the variable stepGoal. + a few a more.
  • Former Member
    Former Member
    Okay. I'm lazy, but here is my function if someone interested.

    function stringReplace(str, oldString, newString)
    {
    var result = str;

    while (true)
    {
    var index = result.find(oldString);

    if (index != null)
    {
    var index2 = index+oldString.length();
    result = result.substring(0, index) + newString + result.substring(index2, result.length());
    }
    else
    {
    return result;
    }
    }

    return null;
    }
  • Same issue here.

    Thanks for the snippet.