APEXA, LLC
Blog Gallery Contact

JavaScript substr(), trim(), starts and endsWith()

Blog Date: Thursday, August 20, 2009 - Discuss below!

 Recent Blogs << Back

Mozilla Firefox "Save & Quit" doesn't work 7/6/2009

How To Ship Wheels AND Tires for cheap 7/9/2009

Trust relationship between workstation and domain 7/13/2009

 More...
 

IT Jobs Hiring


Data Entry: Make $950/Week - Work At Home w/Fortune 500 companies - Nationwide Positions Available — Work At Home Jobs Carlsbad, CA

SENIOR SOFTWARE ENGINEER (mountain view) S San Francisco, CA

Meditech NPR Report Writer Developer (North of Chattanooga) Atlanta North, GA

More jobs...
 

I've gotten a lot into JavaScript and jQuery development lately.

I love jQuery. So simple and powerful. I used to use Prototype, but the ease of development and jQuery documentation makes it the language of choice for me.

Well, in JS, there's always a need for string manipulation. Coming from a managed code world, I expect certain built-in functions. Here is some examples for functions that you wished were in JavaScript.

eg. calling the in-built substr() function would look something like myVar.substr(x, y) but if you wrote your own version (may be function substr2(str, x, y)) you'll have to call it as
 

CODE

myVar = substr2(myVar, x, y); //Your function
myVar = myVar.substr(x, y); //built-in function

Not so elegant is it? 

JavaScript trim() function

Well, here is a way you could BIND your functions to the data type and call them like in-built functions… here's how: 

Trim function, trims all leading and trailing spaces: 

CODE

String.prototype.trim = function(){return
(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}

 

JavaScript startsWith() function

startsWith to check if a string starts with a particular character sequecnce: 

CODE

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

 

JavaScript endsWith() function

endsWith to check if a string ends with a particular character sequecnce:

CODE

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

All these functions once loaded will behave as built-in JavaScript functions.

Here are few examples:

CODE

var myStr = “  Earth is a beautiful planet  ”;
var myStr2 = myStr.trim();  
//==“Earth is a beautiful planet”;

if (myStr2.startsWith(“Earth”)) // returns TRUE

if (myStr2.endsWith(“planet”)) // returns TRUE

if (myStr.startsWith(“Earth”))
// returns FALSE due to the leading spaces…

if (myStr.endsWith(“planet”))
// returns FALSE due to trailing spaces…
 

Pretty slick huh.

 

 

JavaScript substring() Method

JavaScript substring is used to take a part of a string. The syntax of JavaScript substring method is given below:

stringObjectToTakeAPartOf.substring(start-index,stop-index)

  • start-index: (Required - Numeric Value) - where to start the extraction
  • stop-index: (Optional - Numeric Value) - where to stop the extraction

 

Notes about Javascript substring:

  • If start-index is equal to stop-index, JavaScript substring returns an empty string.
  • If stop-index parameter is omitted, JavaScript substring extracts characters from start-index to the end of the string.
  • If parameters are 0or NaN, the parameters are threated as 0.
  • If parameters are greater than the string's length, the parameters qill use string's length.
  • If start-index > stop-index, then the JavaScript substring function swaps 2 parameters.

substring Example:

<script type="text/javascript">

var str = "Hello World";
document.write(str.substring(4,8));

</script>

The output of the above code is:

o wo

JavaScript substr() Method

The JavaScript substr() method works slightly different. Instead of the second parameter being an index number, it gives the number of characters. The syntax of JavaScript substr() is given below:

stringObjectToTakeAPartOf.substr(start-index,length)

  • start-index: (Required - Numeric Value) - where to start the extraction
  • length: (Optional - Numeric Value) - how many characters to extract

Notes about substr:

  • To extract characters from the end of the string, use a negative start-index. (Internet Explorer returns the whole string which is wrong.)
  • If the length parameter is omitted, JavaScript substr method extracts to the end of the string.
  • substr() is not supported by Netscape 2, Netscape 3, Internet Explorer 3 and Opera 3.

JavaScript substr Example:

<script type="text/javascript">

var str = "Hello World";
document.write(str.substr(4,4));

</script>

The output of the above code is:

o wo

Since JavaScript substr() is not cross-browser, I never use it.



Thursday, August 20, 2009 3:01:09 PM

Home | Gallery | Contact | IT Consulting | Forums | Web Marketing | Search Engine Optimization | Web Design & CMS | My Blog on C# .NET

Site Map | Copyright 2007 Web Design web design | Developed by APEXA, LLC

APEXA, LLC