|
||||||
|
||||||
Trimming Leading & Trailing Spaces from Text

Free
Flash Tutorial
![]()
This tutorials will show you how to remove trailing or leading spaces from a string of text, be it a text box, variable, xml loaded file etc. This handy script will trim any string spaces. This includes Tab spaces, Enter or new line spaces and normal spaces form the Space Bar.
It will convert to sentence like:
" This has too many normal spaces at the start and end of it. "
To a sentence like this:
"This has normal spaces in it"
Other Examples might be:
"
This has too many enter spaces at the start of it"
To get a better idea play around with the Flash file:
Removes the extra space at the beginning of the line.Cross-Ref: This will NOT remove any excessive spaces from inside the string, only the spaces at the begging or at the end of the text, or both. To remove excessive spaces from a string see: Excessive Space Remover Tool
Step One: Setting Up the Document
Now you need to check to the publish settings are set to ActionScript 2
Note: This tab will not be visible unless the Flash box is selected:
![]()
Step Two: The Stage
For this to work you will need on the Stage a Text Box to place Text into and a Button to activate the code:
Note: If you take a button from the Common Button Library avoid Knobs, Faders and Component buttons as they work differently.
on (Press) {
// Trim the text
_root.inputField.text = _root.Trim(_root.inputField.text);
/* Remember if you do not want to filter out leading and trailing html space tags add 'false' to the parameters been passed to the Trim function e.g:
_root.Trim(LocationOfTheTextBoxInTheFlashMovie.TheInstanceNameOfTextBoc.text, false); */}
Step Three: The ActionScript
// ========================================================================\\
// >>>>>>>>>>>>>>>>>>>>>>>>KEY GUIDE FOR CODES <<<<<<<<<<<<<<<<<<<<<<<<<<<<\\
// \\
// 13 = A enter space or new line space (same as pressing the enter key) \\
// 32 or 10 = A 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 = "";
// Create a counter
var CharacterCounter = 0;
// Create a variable to hold a string in
var TemporallyHoldAnotherString = "";
// If the function has been called but no value passed to it to see if a
if (GetStringToTrim == 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 a the sting length is grater than 0,
// other wise it's an empty sting and doesn't need to be trimmed
} else {
// Get the string
TemporallyHoldAString = new String(GetStringToTrim);
// Check to ensure filtering html tags is ok
if (FilterOutHTMLCode != false) {
// Convert all html tags for spaces
// This is so that all the different possible ways for having spaces in the text,
// are made into one uniform format. This can then be searched to filter out the
// excessive occurrences of the three main uniformed, types of spaces in flash.
//
// Handles the following html tags:
// <br>,<BR>,</br>,</BR>,<p>,<P>,</p>,</P>,/n,/N,/t,/T,/r,/R, ,&NBSP;
//
// Temporarily convert all commonly occurring closing html tags that also have some of the html
// tags above, in them. We want to remove the above html tags with out effecting the commonly
// according closing html tags. So we temporally convert them so they won't be effected by the
// filter, we can then convert them back afterward
TemporallyHoldAString = TemporallyHoldAString.split("</T").join("|:CT:|");
TemporallyHoldAString = TemporallyHoldAString.split("</N").join("|:NT:|");
TemporallyHoldAString = TemporallyHoldAString.split("</R").join("|:RT:|");
TemporallyHoldAString = TemporallyHoldAString.split("<P ").join("|:P:|");
TemporallyHoldAString = TemporallyHoldAString.split("<p ").join("|:p:|");
TemporallyHoldAString = TemporallyHoldAString.split("</t").join("|:ct:|");
TemporallyHoldAString = TemporallyHoldAString.split("</n").join("|:nt:|");
TemporallyHoldAString = TemporallyHoldAString.split("</r").join("|:rt:|");
//
// Remove Close Tags
// Remove ALL upper case </P> tags as not needed
TemporallyHoldAString = TemporallyHoldAString.split("</P>").join("");
// Remove ALL lower case </p> tags as not needed
TemporallyHoldAString = TemporallyHoldAString.split("</p>").join("");
// Remove ALL upper case </BR> as not needed
TemporallyHoldAString = TemporallyHoldAString.split("</BR>").join("");
// Remove ALL lower case </br> as not needed
TemporallyHoldAString = TemporallyHoldAString.split("</br>").join("");
//
// Convert up case to lower case
// If any upper case /N convert them to lower case /n as uppercase /N tags don't work any way
TemporallyHoldAString = TemporallyHoldAString.split("/N").join("/n");
// If any upper case /R convert them to lower case /r as uppercase /R tags don't work any way
TemporallyHoldAString = TemporallyHoldAString.split("/R").join("/r");
// If any upper case /T convert them to lower case /t as uppercase /T tags don't work any way
TemporallyHoldAString = TemporallyHoldAString.split("/T").join("/t");
// If any upper case <P> convert them to lower case <P>
TemporallyHoldAString = TemporallyHoldAString.split("<P>").join("<p>");
// If any upper case <BR> convert them to lower case <br>
TemporallyHoldAString = TemporallyHoldAString.split("<BR>").join("<br>");
// If any upper case &NBSP; convert them to lower case as uppercase &NBSP; tags don't work any way
TemporallyHoldAString = TemporallyHoldAString.split("&NBSP;").join(" ");
//
// Replace html tags with flash code
// Convert ALL /r enter space's tags to flash enter space's
TemporallyHoldAString = TemporallyHoldAString.split("/r").join(String.fromCharCode(13));
// Convert ALL /n enter space's tags to flash enter space's
TemporallyHoldAString = TemporallyHoldAString.split("/n").join(String.fromCharCode(13));
// Convert ALL /t tab space's tags to flash tab space's
TemporallyHoldAString = TemporallyHoldAString.split("/t").join(String.fromCharCode(9));
// Convert ALL lower case <p> enter space's tags to flash enter space's
TemporallyHoldAString = TemporallyHoldAString.split("<p>").join(String.fromCharCode(13));
// Convert ALL normal space's
TemporallyHoldAString = TemporallyHoldAString.split(" ").join(" ");
// Convert ALL lowercase <br> tags to flash enter space's
TemporallyHoldAString = TemporallyHoldAString.split("<br>").join(String.fromCharCode(13));
//
// Convert the commonly occurring html closing tags back to their original html format
TemporallyHoldAString = TemporallyHoldAString.split("|:CT:|").join("</T");
TemporallyHoldAString = TemporallyHoldAString.split("|:NT:|").join("</N");
TemporallyHoldAString = TemporallyHoldAString.split("|:RT:|").join("</R");
TemporallyHoldAString = TemporallyHoldAString.split("|:ct:|").join("</t");
TemporallyHoldAString = TemporallyHoldAString.split("|:nt:|").join("</n");
TemporallyHoldAString = TemporallyHoldAString.split("|:rt:|").join("</r");
TemporallyHoldAString = TemporallyHoldAString.split("|:P:|").join("<P ");
TemporallyHoldAString = TemporallyHoldAString.split("|:p:|").join("<p ");
}
// For every other type of space convert into one uniform space format to be filtered
TemporallyHoldAString = TemporallyHoldAString.split(String.fromCharCode(160)).join(String.fromCharCode(13));
TemporallyHoldAString = TemporallyHoldAString.split(String.fromCharCode(29)).join(String.fromCharCode(13));
TemporallyHoldAString = TemporallyHoldAString.split(String.fromCharCode(30)).join(String.fromCharCode(13));
TemporallyHoldAString = TemporallyHoldAString.split(String.fromCharCode(31)).join(String.fromCharCode(13));
// Check to see if the string length is long enough to trim
if (TemporallyHoldAString.length<=0) {
// If it not long enough to trim then return the empty string
return "";
// Is no value has been passed to the function then
// 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) && (TemporallyHoldAString.indexOf(String.fromCharCode(10)) == -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 it self
// 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(0) != 10) && (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) != 32) && (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) != 13) && (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) != 9) && (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) != 10)) {
// 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) || (TemporallyHoldAString.charCodeAt(0) == 10)) {
// 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) || (TemporallyHoldAString.charCodeAt(CharacterCounter) == 10)) {
// 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) {
// If their is no spaces at the start of the string then check to see if their are spaces at the end
if ((TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) == 32) || (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) == 13) || (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) == 9) || (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) == 10)) {
// 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) == 10) || (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 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) || (TemporallyHoldAString.charCodeAt(TemporallyHoldAString.length-1) == 10)) {
// 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 ((TemporallyHoldAString.charCodeAt(CharacterCounter) == 13) || (TemporallyHoldAString.charCodeAt(CharacterCounter) == 32) || (TemporallyHoldAString.charCodeAt(CharacterCounter) == 9) || (TemporallyHoldAString.charCodeAt(CharacterCounter) == 10)) {
// 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) {
return TemporallyHoldAString;
// Other wise just return an empty string
} else {
return "";
}
}
}
}
Step Four: Testing the Movie
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
2553 visitors to this page since
March 07 •
|
|
|
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.
|