A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: find and replace characters in string...

  1. #1
    Senior Member fantasio's Avatar
    Join Date
    May 2000
    Location
    at the progress bar
    Posts
    409

    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

  2. #2
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    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

  3. #3
    Senior Member fantasio's Avatar
    Join Date
    May 2000
    Location
    at the progress bar
    Posts
    409

    thanks-a-lot

    that 's great .)

    have to see how long it takes on a long text

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    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

  5. #5
    Junior Member
    Join Date
    Jul 2008
    Posts
    2
    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);

  6. #6
    Senior Member vinayak.kadam's Avatar
    Join Date
    Oct 2006
    Location
    gotoAndPlay("Pune");
    Posts
    831
    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
    As ever,
    Vinayak Kadam

  7. #7
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    i have no idea why anthonykv would dig up a thread from 6 years ago to add nothing to it.

    gparis

  8. #8
    Junior Member
    Join Date
    Jul 2008
    Posts
    2
    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

  9. #9
    FlashImpulse.com
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    60
    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.
    FlashImpulse.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center