Solved

This pointer JavaScript reference (complex)

Posted on 2003-10-22
3
758 Views
Last Modified: 2008-03-06
Experts,

I am making a generic trim function for JavaScript and extending this trim function the String object in JavaScript.

I have two functions that do the trim.  rtrim and ltrim.  As the names imply they trim the right and left sides of the string.  However, since I am extending this function to the JavaScript String object I am using the this pointer to pass information between the two functions.  But, it does not appear to work.

--Code Begin---
String.prototype.trim=function()
{
   var trimedStr = this.rtrim(this.ltrim());
   return trimedStr;
}  

String.prototype.ltrim=function()
{
  var trimChars = new Array(9,10,13,32);
  var trimStrLen = this.length;
  var startidx = 0;
  var trimedStr = "";
 
  for (var i=0;i<trimStrLen;i++)
  {      
    if (this.charCodeAt(i) != trimChars[0] && this.charCodeAt(i) != trimChars[1] &&
        this.charCodeAt(i) != trimChars[2] && this.charCodeAt(i) != trimChars[3])
    {
      startidx = i;
      break;
    }
  }  
 
  return this.substring(startidx,trimStrLen);
}

String.prototype.rtrim=function()
{
  var trimChars = new Array(9,10,13,32);
  var trimStrLen = this.length-1;
  var startidx = trimStrLen;
  var trimedStr = "";
 
  for (var i=trimStrLen;i>0;i--)
  {  
     if (this.charCodeAt(i) != trimChars[0] && this.charCodeAt(i) != trimChars[1] &&
        this.charCodeAt(i) != trimChars[2] && this.charCodeAt(i) != trimChars[3])
    {
      startidx = i;
      break;
    }  
  }  
   
  return this.substring(0,startidx+1);
}

var mytestvar = " -teststring- ";
var trimstr;

alert(mytestvar.length);
trimstr = mytestvar.trim();
alert(trimstr.length);
--Code End--

If I change the (trimstr = mytestvar.trim();) to (trimstr = mytestvar.rtrim(); or trimstr = mytestvar.ltrim();) then the string comes back trimed. But, not when I use the main function that is supposed to call both routines.

Any ideas?
-mmetzger
0
Comment
Question by:mmetzger
3 Comments
 

Author Comment

by:mmetzger
ID: 9607721
Solution was found on my own.

<b>Please refund my points EE.</b>

Solution as follows: posted by mmetzger

function trim(trimStr)
{    
   var trimedStr = rtrim(ltrim(trimStr));
   return trimedStr;
}  

function ltrim(trimStr)
{
  var trimStrLen = trimStr.length;
   
  for (var i=0;i<trimStrLen;i++)
  {
    var aCharCode = trimStr.charCodeAt(i);
   
    if (isWhiteSpace(aCharCode) == false)
    {
       break;
    }
  }  
 
  return trimStr.substring(i,trimStrLen);
}

function rtrim(trimStr)
{
  var trimStrLen = trimStr.length-1;
   
  for (var i=trimStrLen;i>0;i--)
  {
    var aCharCode = trimStr.charCodeAt(i);
   
    if (isWhiteSpace(aCharCode) == false)
    {
      break;
    }      
  }  
 
  return trimStr.substring(0,i+1);
}

function isWhiteSpace(possWsChar)
{
  /* White space chars
  9  = TAB
  10 = LF (line feed)
  12 = FF (form feed)
  13 = CR (Carriage return)
  32 = Space */

  var trimChars = new Array(9,10,12,13,32);
  var WsFound = false;
   
  for (var i=0;i<trimChars.length;i++)
  {    
    if (possWsChar == trimChars[i])
    {
      WsFound = true;
    }
  }
 
  return WsFound;  
}
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10345811
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 10377120
PAQed, with points refunded (50)

Computer101
E-E Admin
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sorting a string 1 27
https://github.com/willowsystems/jSignature 11 46
Javasctipt 2 24
Javascript Form Change 5 13
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now