-
Senior Member
find and replace characters in string...
(F5)
What's the best way to find and replace a certain character from within a string, that I got from user input ?
say i want to replace all "ä" with ae and vice versa.
thanks
-
here's a function. Credit to Colin Moock:
function replace (origStr, searchStr, replaceStr) {
var tempStr = "";
var startIndex = 0;
if (searchStr == "") {
return origStr;
}
if (origStr.indexOf(searchStr) != -1) {
while ((searchIndex = origStr.indexOf(searchStr, startIndex)) != -1) {
tempStr += origStr.substring(startIndex, searchIndex);
tempStr += replaceStr;
startIndex = searchIndex + searchStr.length;
}
return tempStr + origStr.substring(startIndex);
} else {
return origStr;
}
}
usage:
result = replace(_root.output, "ä", " æ");
gparis
-
Senior Member
thanks-a-lot
that 's great .)
have to see how long it takes on a long text
-
Hi,
I recently ran into the problem that this code created annoying "computer slow" nessages when searching a few thousand characters with FP5.
I changed the search function to loop over the text and use substring, instead of that indexOf, and all problems were gone.
It was working fine with FP6 and the indexOf thingy
Musicman
-
hi ,
here is a Recursive Function to replaceString
ReplaceAll(searchStr:String,replaceStr :String,origStr:String):String
{
if (origStr.indexOf(searchStr) != -1)
origStr = origStr.replace(searchStr,replaceStr);
if (origStr.indexOf(searchStr)== -1)
return origStr;
else
return ReplaceAll(searchStr,replaceStr ,origStr);
}
usage:
result = ReplaceAll("ä", " æ",strSource);
-
Senior Member
Well, I dont think that your logic would make a large difference to that mentioned by gParis.... this will also approximately take same time to ouput the expected thing.
gParis, We expect an answer from u.
As ever,
Vinayak Kadam
-
i have no idea why anthonykv would dig up a thread from 6 years ago to add nothing to it.
gparis
-
Hi there,
I am new to actionscript . I have started coding in action script just 3 months before.
i was searching in google for a code in action script which can replace a character of multiple occurence with a particular charaacter in a source string and found gParis"s code.
In fact i am using gParis"s code my project too.
Itz only about that after seeing this code i thought we can do it other way also and just posted it . i didnt mention that my code is better than that .
i know that interative tends to be faster since it does not have to switch stacks so often and recursive is suggested for easier looking code
-
Code:
// Usage: replaceChar("Hello World", "o", "k");
// Return: Hellk Wkrld
function replaceChar(s:String, a:String, b:String):String {
var ar:Array = s.split(a);
s = "";
for(var i:Number = 0; i < ar.length; i++) s += i < ar.length-1 ? ar[i]+b : ar[i];
return s;
}
I realise this thread is years old but here's the best method I've come up with.
It creates an array by splitting the text wherever the character occurs (and simultaneously removes it), then it simply prints the array to a blank string, and puts the new character inbetween.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|