Flash Tutorials: Space Remover

Home Food Mates • Members Tutorials Forum Buy Templates Contact Us 

 


Flash Tutorials

   

Flash Tutorial - Excess Space Remover

 

Free Flash Tutorial


Step 2    <<   Previous      Intro   1   2   3     Next   >>       >>   Webwasp Mates

 

Step Two: The Script

  1. Past the following code into a frame:
    // Create a function called 'Trim' that we can pass a string to the i.e. _roor.MyTextBox.text = _root.Trim(_roor.MyTextBox.text);
    function Trim(GetStringToTrim) {

    // ======================================================================= \\ // >>>>>>>>>>>>>>>>>>>>>>>KEY GUIDE FOR CODES <<<<<<<<<<<<<<<<<<<<<<<<<<<<         \\ //                                                                                                                                                                  \\ // 13 = A enter space or new line space (same as pressing the enter key)                                                 \\ // 32 = A space or normal space (same as pressing the space bar)                                                             \\ // 9 = A tab space(>same as pressing the tab button in Microsoft Word)                                                    \\ //                                                                                                                                                                   \\ // ========================================================================\\ // // Get the string to trim var TemporallyHoldAString = String(GetStringToTrim); // Create a counter var CharacterCounter = 0;
    // Create a variable to hold a string in
    var TemporallyHoldAnotherString = "";
    // Check a the sting length is grater than 0,
    // other wise it's an empty sting and doesn't need to be trimmed
    if (TemporallyHoldAString.length<=0) {
    // return the empty string
    return ""; // Is no value has been passed to the function then
    } else if (TemporallyHoldAString == "undefined") {
    // Alert the user, only works when testing in Flash not as a swf
    // You may wish, if deploying this on a web page or stand alone application;
    // to display an error message to the user or have some code to report that their
    // is a fault with your application back to you, via e-mail
    // If you see this error message it just means their is a problem with how you
    // have called this function, and you are not passing anything to it, for it to work with.
    // To fix just ensure that your passing a string to the function. If you are passing a string from an textbox,
    // be sure that it exists in the flash movie and ensure you have the instance name and target path correct
    trace("The 'Trim' function has been called but no value has been passed to it for processing, a empty string has been returned as a fail safe"); // return an empty string
    return "";
    // Check their are spaces to be removed in the string
    } else if ((TemporallyHoldAString.indexOf(String.fromCharCode(13)) == -1) && (TemporallyHoldAString.indexOf(String.fromCharCode(32)) == -1) && (TemporallyHoldAString.indexOf(String.fromCharCode(9)) == -1)) { // // If their are not, then return an empty string
    return TemporallyHoldAString;
    // If their are spaces in the string but not at the beginning
    // or end of the string, then the spaces must be in the string itself
    // in which case we doesn't need to trim the text
    } else if ((TemporallyHoldAString.charCodeAt(0) != 32) && (TemporallyHoldAString.charCodeAt(0) != 13) && (TemporallyHoldAString.charCodeAt(0) != 9) && (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) != 32) && (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) != 13) &&(TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) != 9)){/ // return the string
    return TemporallyHoldAString;
    // Check to see if the spaces are at the start of the sting
    } else if ((TemporallyHoldAString.charCodeAt(0) == 32) || (TemporallyHoldAString.charCodeAt(0) == 13) || (TemporallyHoldAString.charCodeAt(0) == 9)) {
    // If they are then set the counter to 0
    CharacterCounter = 0;
    // Clear the variable
    TemporallyHoldAnotherString = "";
    // Start counting through the string
    while (CharacterCounter<=TemporallyHoldAString.length) {
    // If their is a space then
    if ((TemporallyHoldAString.charCodeAt(CharacterCounter) == 13) ||(TemporallyHoldAString.charCodeAt(CharacterCounter) == 32) || (TemporallyHoldAString.charCodeAt(CharacterCounter) == 9)){
    // Keep counting
    CharacterCounter = CharacterCounter+1;
    // If we hit a character which is not a space then
    } else {
    // Cut the spaces out up to that character from the start of the string
    TemporallyHoldAnotherString = TemporallyHoldAString.slice(CharacterCounter, TemporallyHoldAString.length);
    // Exit the loop
    break;
    }
    }
    // If we have cut the spaces all away and their is a sting still left, this means
    // their are spaces possible at the end of the sting to but cut away too
    if (TemporallyHoldAnotherString.length>0) {
    // Set the counter from the end of the string
    CharacterCounter = TemporallyHoldAnotherString.length-1;
    // Clear the variable
    TemporallyHoldYetAnotherString = "";
    // Start looping backwards through the string
    while (CharacterCounter>=0){
    // If their is a space then
    if ((TemporallyHoldAnotherString.charCodeAt(CharacterCounter) == 13) || (TemporallyHoldAnotherString.charCodeAt(CharacterCounter) == 32) || (TemporallyHoldAnotherString.charCodeAt(CharacterCounter) == 9)){
    // Keep counting
    CharacterCounter = CharacterCounter-1;
    // If we hit a character which is not a space then
    } else {
    // Cut the spaces out from the end of the string to the start of the character
    TemporallyHoldYetAnotherString = TemporallyHoldAnotherString.slice(0, CharacterCounter+1);
    // return the string
    return TemporallyHoldYetAnotherString;
    // Exit the loop
    break;
    }
    }
    // If their were just were no spaces at the end, but just at the start of the sting
    } else {
    // return the sting as now we have removed all the leading spaces
    return TemporallyHoldAnotherString;
    }
    // If their is no spaces at the start of the string then check to see if their are spaces at the end
    } else if ((TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) == 32) || (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) == 13) || (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) == 9)) {
    // Set the counter from the end of the string
    CharacterCounter = TemporallyHoldAString.length-1;
    // Clear the variable
    TemporallyHoldAnotherString = "";
    // Start looping backwards through the string
    while (CharacterCounter>=0){
    // If their is a space then
    if ((TemporallyHoldAnotherString.charCodeAt(CharacterCounter) == 13) || (TemporallyHoldAnotherString.charCodeAt(CharacterCounter) == 32) || (TemporallyHoldAnotherString.charCodeAt(CharacterCounter) == 9)){
    // Keep counting
    CharacterCounter = CharacterCounter-1;
    // If we hit a character which is not a space then
    } else {
    // Cut the spaces out from the end of the string to the start of the character
    TemporallyHoldAnotherString = TemporallyHoldAString.slice(0, CharacterCounter+1);
    // return the string
    return TemporallyHoldAnotherString;
    // Exit the loop
    break;
    }
    }
    // If some how it all goes tits up then hopefully this line will
    // at least tell you that it's all gone tits up but still allow the
    // code to continue working
    } else {
    // Alert the user, only works when testing in Flash not as a swf
    // You may wish, if deploying this on a web page or stand alone application;
    // to display an error message to the user or have some code to report that their
    // is a fault with your application back to you, via e-mail
    // If you see this error message it just means their is a problem with how you
    // have called this function, and you are not passing anything to it, for it to work with.
    // To fix just ensure that your passing a string to the function. If you are passing a string from an textbox,
    // be sure that it exists in the flash movie and ensure you have the instance name and target path correct
    // If you see this.....then ill be surprised as technically its impossible to get this error message because of
    // the way the code works, so if you see this....take out a hammer at try threatening your pc! You will be surprised
    // how often that works =0/ Also some times I find that unexplained bugs in Flash crop up, So try copying all the code
    // and everything else, to a new flash file. That some times fixes things
    trace("An unexpected error has some how occurred! If you have altered the code in any way please change it back. You should also check out you have called the 'Trim' function and ensure that you are passing a value to it; called the function in the correct manner. A empty string has been returned as a fail safe. If you are seeing this error message as part of a web page/standalone application, please report the error to the application distributors. For further help and advice regarding this error, please go to http://www.webwasp.co.uk/ and post a request for help on the web sites forums."); // Try to return the sting passed to function
    if (TemporallyHoldAString != undefined && TemporallyHoldAString != "undefined") {
    return TemporallyHoldAString;
    // Other wise just return an empty string
    } else {
    return "";
    }
    }
    }

         

Step 2    <<   Previous      Intro   1   2   3     Next   >>       >>   Webwasp Mates


Webwasp Community: Webwasp Mates & Dates

Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates

2496 visitors to this page since Feb 07 •

 Top of Page Home Food Mates Members Tutorials Forum Buy Templates Contact Us 
 All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.