mmetzger
asked on
This pointer JavaScript reference (complex)
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=func tion()
{
var trimedStr = this.rtrim(this.ltrim());
return trimedStr;
}
String.prototype.ltrim=fun ction()
{
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,tr imStrLen);
}
String.prototype.rtrim=fun ction()
{
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
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=func
{
var trimedStr = this.rtrim(this.ltrim());
return trimedStr;
}
String.prototype.ltrim=fun
{
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,tr
}
String.prototype.rtrim=fun
{
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+
}
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
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
<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,trimSt
}
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;
}