Home Tutorials Forums Articles Blogs Movies Library Employment Press

<< Prev 5 | Next 5

Finds a string in a string and replace it with another string without modifiying the original

//_root.mystring="jaaaaaaaazzzz....."
//_root.nynewstring=_root.mystring.searchreplace("a","e")

String.prototype.searchreplace=function(find,replace) {
        var string=this
        var counter
        while (counter<string.length) {
                var start = string.indexOf(find, counter);
                if (start == -1) {
                        break;
                } else {
                        var before=string.substr(0,start)
                        var after=string.substr(start+find.length,string.length)
                        string=before+replace+after
                        var counter=before.length+replace.length
                        
                }
        }
        return string;
}

Posted by: Flashguru | website http://www.flashguru.co.uk/
Finds a string in a string and replace it with another string without modifiying the original (2)
// this version is 15x faster than the first version.

String.prototype.searchReplace=function(find,replace) {
        return this.split( find ).join( replace);
}

var myString = "Jaaaaaaaaaaaaazzzz...";
trace(myString);// Jaaaaaaaaaaaaazzzz...
var newString = myString.searchReplace("a","e");
trace( newString ); // Jeeeeeeeeeeeeezzzz...

Posted by: manix | website http://ashita-studio.com?actionscript.org
firstCasetoUppercase
/*
Converts the first cháracter into Uppercase and the rest into Lowercase

usefull for textinputs to get the same output

example > colonge, cOLOgne, ... becomes Cologne


*/


String.prototype.firstCasetoUppercase = function() {
        return (this.charAt(0).toUpperCase()+this.toLowerCase().slice(1, this.length));
};
// usage
var eingabe = ["berlin", "aachen", "köln"];
for (var i = 0; i<eingabe.length; i++) {
        trace(eingabe[i].firstCasetoUppercase());
}

Posted by: andihaas | website http://www.flash.andihaas.de
format phone number text entry
=========================================================================|
// formats phone number entry text to get rid of dashes,
// parentheses, decimals for and formats properly to Unites States Style
// by Geordie Martinez (there is an easier way to do it, I'm sure.)
// http://www.thenewchannel.tv/
=========================================================================|

function formatPhoneNumber(phoneNum) {
        myString = phoneNum.toString();
        containsBlankSpace = myString.indexOf(" ");
        containsOpenParen = myString.indexOf("(");
        containsCloseParen = myString.indexOf(")");
        containsDecimalPoint = myString.indexOf(".");
        containsDash = myString.indexOf("-");
        // =================checks for Blank Space ====================
        if (containsBlankSpace == -1) {
                // no Open parenthesis
        } else {
                // contains open parenthesis
                // get rid of them for the time being
                bsNumbers = myString.split(" ");
                tempbsString = "";
                for (bs = 0; bs < bsNumbers.length; bs++) {
                        tempbsString += bsNumbers[bs];
                }
                myString = tempbsString;
                trace("removed open parenthesis and returned this: " add myString);
        }
        // =================checks for open Parenthesis ====================
        if (containsOpenParen == -1) {
                // no Open parenthesis
        } else {
                // contains open parenthesis
                // get rid of them for the time being
                opNumbers = myString.split("(");
                tempopString = "";
                for (op = 0; op < opNumbers.length; op++) {
                        tempopString += opNumbers[op];
                }
                myString = tempopString;
                trace("removed open parenthesis and returned this: " add myString);
        }
        // =================checks for close Parenthesis ====================
        if (containsCloseParen == -1) {
                // no
        } else {
                // contains close parenthesis
                // get rid of them for the time being
                cpNumbers = myString.split(")");
                tempcpString = "";
                for (cp = 0; cp < cpNumbers.length; cp++) {
                        tempcpString += cpNumbers[cp];
                }
                myString = tempcpString;
                trace("removed close parenthesis and returned this: " add myString);
        }
        // ============ checks for decimal points =========================
        if (containsDecimalPoint == -1) {
                // no
        } else {
                // contains close parenthesis
                // get rid of them for the time being
                dpNumbers = myString.split(".");
                tempdpString = "";
                for (dp = 0; dp < dpNumbers.length; dp++) {
                        tempdpString += dpNumbers[dp];
                }
                myString = tempdpString;
                trace("removed decimals and returned this: " add myString);
        }
        // =================checks for dashes ====================
        if (containsDash == -1) {
                // no commas
        } else {
                // contains commas
                // get rid of them for the time being
                dashNumbers = myString.split("-");
                tempDashString = "";
                for (da = 0; da < dashNumbers.length; da++) {
                        tempDashString += dashNumbers[da];
                }
                myString = tempDashString;
                trace("removed open parens and returned this: " add myString);
        }
        // ================================================================
        // should just be a number now.
        // now we make an array and pop the proper items into place.
        // ================================================================
        formatMeProperly = myString.split("");
        
        
        if (formatMeProperly.length == 10){
                //  with area code
                trace ("long distance");
                finalProduct = "(" + formatMeProperly[0] + formatMeProperly[1] + formatMeProperly[2] +") " + formatMeProperly[3] + formatMeProperly[4] + formatMeProperly[5] + "-" + formatMeProperly[6] + formatMeProperly[7] + formatMeProperly[8] + formatMeProperly[9];
                return finalProduct;
                
        } else if (formatMeProperly.length == 7){
                //  no area code
                trace ("no area code");
                finalProduct = "" + formatMeProperly[0] + formatMeProperly[1] + formatMeProperly[2] +"-" + formatMeProperly[3] + formatMeProperly[4] + formatMeProperly[5] +  formatMeProperly[6];
                return finalProduct;
        } else {
                // wrong number of characters
                trace ("too many or too few digits");
                return "re-enter number";
        }
        
        
}

Posted by: Geordie Martinez | website http://www.thenewchannel.tv
Format String
// input : format these     string
// output : format these string


var oldString ="format these     string"; // the existing string
var stringLength = oldString.length; // count the characters in the existing string
trace("Old String ="+oldString); // output the existing string
newString = ""; // set a new string value
for (i=0; i<stringLength; i++) { // loop through the characters of the existing string
        if ((oldString.charAt(i)==" ") and (oldString.charAt(i+1)==" ")) { // find a particular  character and omit it from the loop
                
        }
        else
        {
                newString = newString+oldString.charAt(i);
        }
}
trace("New String ="+newString); // output the new string

Posted by: Trinh Nguyen Vu | website http://www.smartweb.vn

<< Prev 5 | Next 5

Copyright 2000-2010 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.