Sunday, May 11, 2008

String

Description


"codeapedia.string.js" prototypes the String class to add every
day functionality and class properties.



Code


/**
* @projectDescription String for Javascript Library
*
* @author Ryan Estes http://www.codeapedia.com
* @version 0.1
*/

/**
* Determines whether the specified string occurs in
* within this string.
* @return {Boolean}
*/

String.prototype.contains = function(pValue)
{
return this.indexOf(pValue) != -1;
}

/**
* Determines whether the end of this string matches
* the specified string.
* @return {Boolean}
*/

String.prototype.endsWith = function(pValue,pBlnIgnoreCase)
{
var strSource = this;
var strValue = pValue;
if(pBlnIgnoreCase)
{
strSource = strSource.toLowerCase();
strValue = strValue.toLowerCase();
}
return strSource.indexOf(strValue) ==
(strSource.length - strValue.length);
}

/**
* Inserts a string within this string at the specified index
* @return {String}
*/

String.prototype.insert = function(pValue,pIndex)
{
if(pValue == null)
{
throw "Argument null exception";
}
if(pIndex < 0 || pIndex >= this.length)
{
throw "Argument out of range exception";
}
if(pIndex == 0)
{
return pValue + this;
}
else if (pIndex == this.length - 1)
{
return this + pValue;
}
else
{
var strStart = this.substring(0,pIndex);
var strEnd = this.substring(pIndex,this.length);
return strStart + pValue + strEnd;
}
}

/**
* Pads this string on the left with a specified number
* of character strings.
* @return {String}
*/

String.prototype.padLeft = function(pValue,pCount)
{
var strLeft = "";
for(var i = 0; i < pCount; i++)
{
strLeft += pValue;
}
return strLeft + this;
}

/**
* Pads this string on the right with a specified number
* of character strings.
* @return {String}
*/

String.prototype.padRight = function(pValue,pCount)
{
var strRight = "";
for(var i = 0; i < pCount; i++)
{
strRight += pValue;
}
return this + strRight;
}

/**
* Removes all carriage returns and new lines from this string
* @return {String}
*/

String.prototype.removeLines = function()
{
return this.replace(/(\n\r|\n|\r)/gm,"");
}

/**
* Determines whether the start of this string matches
* the specified string.
* @return {Boolean}
*/

String.prototype.startsWith = function(pValue,pBlnIgnoreCase)
{
var strSource = this;
var strValue = pValue;
if(pBlnIgnoreCase)
{
strSource = strSource.toLowerCase();
strValue = strValue.toLowerCase();
}
return strSource.indexOf(strValue) == 0;
}

/**
* Removes white space from this string
* @return {String}
*/

String.prototype.trim = function()
{
return this.replace(/^\s+|\s+$/g,"");
}

/**
* Removes white space from the start of this string
* @return {String}
*/

String.prototype.trimLeft = function()
{
return this.replace(/^\s+/,"");
}

/**
* Removes white space from the end of this string
* @return {String}
*/

String.prototype.trimRight = function()
{
return this.replace(/\s+$/,"");
}

var String =
{
/**
* Determines whether two strings specified are equal
* @return {Boolean}
*/

compare : function(pStringA,pStringB,pBlnIgnoreCase) {
var strA = pStringA;
var strB = pStringB;
if(pBlnIgnoreCase)
{
strA = strA.toLowerCase();
strB = strB.toLowerCase();
}
return strA == strB;
},

// Empty string

empty : "",

/**
* Determines whether the specified string is
* null or empty
* @return {Boolean}
*/

isNullOrEmpty : function (pValue) {
return pValue == null ||
pValue == String.empty;

},

/**
* Converts the specified string to a floating value.
* Returns minimum float value if fails.
* @return {Float}
*/

toFloat : function(pValue) {
var strValue = pValue;
if(pValue == null || isNaN(pValue))
{
strValue = "-3.402823E+38";
}
return parseFloat(strValue);
},

/**
* Converts the specified string to an integer value.
* Returns minimum integer value if fails.
* @return (Int}
*/

toInt : function(pValue) {
var strValue = pValue;
if(pValue == null || isNaN(pValue))
{
strValue = "-2147483648";
}
return parseInt(strValue);
}
}


Examples

Code section below alerts "true".
// demonstrates contains
var str = "ASP.NET";
alert(str.contains("."));

Code section below alerts "true".
// demonstrates endsWith
alert(str.endsWith("NET",true));

Code section below alerts "ASP.NET2.0".
// demonstrates insert
alert(str.insert("2.0",str.length - 1));

Code section below alerts "BeginningASP.NET".
// demonstrates padLeft
alert(str.padLeft("Beginning",1));

Code section below alerts "ASP.NET3.0".
// demonstrates padRight
alert(str.padRight("3.0",1));

Code section below alerts "ASP.NET3.0".
// deomstrates removeLines
alert("\r\n\r\nASP.NET\r3.0".removeLines());

Code section below alerts "false".
// demonstrates startsWith
alert(str.startsWith("asp.net"));

Code section below alerts "4","5","5" respectively.
// demonstrates trim functions
var str = " 2345 ";
alert(str.trim().length);
alert(str.trimLeft().length);
alert(str.trimRight().length);

Code section below alerts "true".
// demonstrates String.empty
alert(" ".trim() == String.empty);

Code section below alerts "true".
// demonstrates String.compare
alert(String.compare("A","a",true));

Code section below alerts "true".
// demonstrates String.isNullOrEmpty
alert(String.isNullOrEmpty(String.empty));

Code section below alerts "-3.402823E+38".
// demonstrates String.toFloat
alert(String.toFloat("ASP"));

Code section below alerts "2".
// demonstrates String.toInt
alert(String.toInt("2.0"));

0 comments:

 

blogger templates | Make Money Online