String replace function

About advanced scenes, and the Thyme scripting language used in Algodoo.

String replace function

Postby TheLeandroNex01 » Tue Feb 03, 2015 9:11 pm

Hello there,
i'm trying to make a String Replace Function.
like so :
_StringReplace(Original, ToReplace, ReplaceValue);
Example :
_StringReplace("1 + 1 = 0", "+", "-");
Result :
"1 - 1 = 0"

but, it doesn't work.
My Code :
Spoiler: show
Code: Select all
(Original, ToReplace, ReplaceValue)=>{
    arrayOriginal = string.str2list(Original);
   arrayToReplace = string.str2list(ToReplace);
   arrayReplaceValue = string.str2list(ReplaceValue);
   indexToReplaceStart = (-1);
   indexToReplaceEnd = (-1);
   for(string.length(Original), (i)=>{
      A = true;
      for(string.length(ToReplace), (j)=>{
         A = (A ? (arrayOriginal(i+j) == arrayToReplace(j)) : false);
      });
      indexToReplaceStart = (A ? i : (-1));
   });
   indexToReplaceEnd = indexToReplaceStart + string.length(ToReplace);
   newString = Original;
   if(indexToReplaceStart != (-1), {
      strA = "";
      for(indexToReplaceStart, (i)=>{
         strA = strA + arrayOriginal(i);
      });
      strB = "";
      val = (string.length(Original) - string.length(ToReplace)) - string.length(strA);
      val = val * (val < 0 ? (-1) : 1);
      for(val, (i)=>{
         strB = strB + arrayOriginal(i + indexToReplaceEnd);
      });
      newString = strA + ReplaceValue + strB;
   });
   newString;
}


Can you help me ?
Last edited by TheLeandroNex01 on Wed Feb 04, 2015 12:42 am, edited 2 times in total.
TheLeandroNex01
 
Posts: 11
Joined: Mon Jan 19, 2015 3:20 am

Re: String replace function

Postby Kilinich » Wed Feb 04, 2015 12:05 am

Here you are: set of functions to select substring, search substring and replace.

Code: Select all
scene.my.subStr = (str, from, len) => {
  l := string.str2list(str);
  sub := [];
  for(len, (i) => {sub = sub ++ [l(i+from)]});
  string.list2str(sub)
};

scene.my.findStr = (whereStr, whatStr) => {
  wherePos := [];
  for(string.length(whereStr) - string.length(whatStr)+1, (i) => {
    scene.my.subStr(whereStr, i, string.length(whatStr)) == whatStr ? {wherePos = wherePos ++ [i]} : {}
  });
  wherePos
};

scene.my.replaceStr = (whereStr, whatStr, toStr) => {
  wherePos := scene.my.findStr(whereStr,whatStr);
  dest := ""; pos := 0;
  for(string.length(wherePos), (i)=>{
    dest = dest + scene.my.subStr(whereStr, pos, wherePos(i) - pos) + toStr; 
    pos = wherePos(i) + string.length(whatStr)
  });
  dest + scene.my.subStr(whereStr, pos, string.length(whereStr) - pos)
};
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: String replace function

Postby TheLeandroNex01 » Wed Feb 04, 2015 12:16 am

Kilinich wrote:Here you are: set of functions to select substring, search substring and replace.

Code: Select all
scene.my.subStr = (str, from, len) => {
  l := string.str2list(str);
  sub := [];
  for(len, (i) => {sub = sub ++ [l(i+from)]});
  string.list2str(sub)
};

scene.my.findStr = (whereStr, whatStr) => {
  wherePos := [];
  for(string.length(whereStr) - string.length(whatStr)+1, (i) => {
    scene.my.subStr(whereStr, i, string.length(whatStr)) == whatStr ? {wherePos = wherePos ++ [i]} : {}
  });
  wherePos
};

scene.my.replaceStr = (whereStr, whatStr, toStr) => {
  wherePos := scene.my.findStr(whereStr,whatStr);
  dest := ""; pos := 0;
  for(string.length(wherePos), (i)=>{
    dest = dest + scene.my.subStr(whereStr, pos, wherePos(i) - pos) + toStr; 
    pos = wherePos(i) + string.length(whatStr)
  });
  dest + scene.my.subStr(whereStr, pos, string.length(whereStr) - pos)
};

Thank you, do you know how to make something like:
string.toLower("A");
result :
a
?
TheLeandroNex01
 
Posts: 11
Joined: Mon Jan 19, 2015 3:20 am


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 4 guests