Link to home
Start Free TrialLog in
Avatar of splendorx
splendorx

asked on

Flash Trim Function

I am trying to trim a string with extra white space and line breaks.  I a have tried using the following AS from:

https://www.experts-exchange.com/questions/20294500/Trim-the-spaces-off-a-string.html?query="flash+trim"&clearTAFilter=true

Example:

function LTrim(str) {
      var whitespace = new String(" \t\n\r");
      var s = new String(str);
      if (whitespace.indexOf(s.charAt(0)) != -1) {
            var j = 0, i = s.length;
            while (j<i && whitespace.indexOf(s.charAt(j)) != -1) {
                  j++;
            }
            s = s.substring(j, i);
      }
      return s;
}
function RTrim(str) {
      var whitespace = new String(" \t\n\r");
      var s = new String(str);
      if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
            var i = s.length-1;
            while (i>=0 && whitespace.indexOf(s.charAt(i)) != -1) {
                  i--;
            }
            s = s.substring(0, i+1);
      }
      return s;
}
function Trim(str) {
      return LTrim(RTrim(str));
}


myString = "hello world   ";
trace(myString);
Trim(myString);
trace(str);

The str trace comes back as undefined.  Is there something wrong with the syntax?  Any other suggestions for a trim function?

TIA,

Terry
ASKER CERTIFIED SOLUTION
Avatar of AgentSmith007
AgentSmith007

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of splendorx
splendorx

ASKER

Thanks for the help!!
Glad to help!