String replace function
3 posts • Page 1 of 1
String replace function
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 :
Can you help me ?
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
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
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...
-

Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: String replace function
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
3 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 4 guests



