When you write JavaScript, you need to know what string manipulation methods/functions are available.

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)

 

Notes about Javascript substring:


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)


Notes about substr:


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.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 4.2 by 10 people

  • Currently 4.2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5




Comments

November 21. 2007 15:33

I knew in the back of my head there was a difference. I'm guilty of using both though... now I know the difference. I found value to this post, thanks for taking the time.

Casey Wise

November 21. 2007 17:11

substr is cross browser, *today*,
The rulers today are: IE6, FF 2, Safari 2, Opera 8.5 and they support substr..
you can use substr as much as you want.


These browser's versions that you mentioned are not used anymore by anyone

Nir Tayeb

November 21. 2007 17:20

You are right, Nir.

However, if we talk about cross-browser javascript, it is important to mention the browser versions that are supported.

Blogging Developer

December 29. 2007 23:27

Yeah, that is a way overboard interpretation of "cross-browser." The phrase does not mean "backward-compatible;" that is an impossibility these days. At the very earliest, IE 5.5 could try to be supported, but anything before that is obsolete. Otherwise, we'd be stuck without things like DOM functions or XMLHttpRequest.

Kyle

July 11. 2008 07:34

this example was very benificial and solved problem of mine..
actully on many sites stbstring () function is given not substr() i want that .. so it was really good

pankaj

Add comment





(will not be displayed)








  • javascript
  • querystring
  • url parameters
  • parse querystring
  • delayed redirect
  • settimeout
  • focus
  • textbox
  • page load
  • after submit
  • set focus in asp.net 2.0/3.5
  • on page load
  • after postback
  • set control focus
  • postback
  • asp.net 1.x
  • substring
  • substr
  • javascript string methods
  • parsefloat
  • convert strings to numbers
  • parseint
  • javascript history
  • history.go
  • history.back
  • http requests
  • image maps
  • css sprites
  • external css
  • external javascript
  • compress javascript
  • javascript compression
  • ajaxcontroltoolkit
  • tab control
  • array
  • length
  • javascipt
  • lastmodified
  • mstsc
  • terminal services
  • remote desktop connections
  • null
  • undefined
  • array.join
  • string concatenation
  • setinterval
  • clearinterval
  • timing events
  • cleartimeout
  • javascript timing events
  • url redirection
  • location.href
  • location.replace
  • redirect
  • redirection
  • system.io.compression
  • viewstate compression
  • compress viewstate
  • gzipstream
  • loadpagestatefrompersistencemedium
  • savepagestatetopersistencemedium
  • form spam
  • captcha
  • prevent spam without captcha
  • url redirect
  • defaultbutton
  • enter key
  • default button
  • asp.net
  • 2.0
  • form
  • panel
  • 1.1
  • form submit
  • dopostback
  • onkeypress
  • onkeydown
  • onkeyup
  • javascript key events
  • keycode 13
  • disable enter key
  • int32.parse
  • convert.toint32
  • int32.tryparse
  • google
  • hoax
  • gmail
  • storage
  • counter
  • mail
  • visual studio 2005
  • vs 2008
  • copy
  • paste
  • clipboard data
  • static variables
  • application object
  • static property
  • server control
  • web file manager
  • iz web file manager
  • convert
  • parse
  • tryparse
  • file upload control
  • maxrequestlength
  • executiontimeout
  • httpruntime
  • asp.net 2.0
  • registering scripts
  • registerclientscript
  • registerstartupscript
  • cross-browser
  • events
  • improve web site performance
  • compression
  • caching
  • elmah
  • error logging
  • exception
  • error
  • httphandler
  • google sitemap generator
  • sitemap
  • internet information services
  • iis7
  • hosts file
  • localhost
  • windows vista
  • search engine optimization
  • seo
  • search engine friendly pages
  • headscriptmanager
  • class library
  • head
  • css
  • c#
  • iis
  • internet information services manager
  • 401.3 unauthorized
  • 500.0 internal server error
  • http error 500.19
  • google toolbar
  • yellow input fields
  • input
  • select
  • background color
  • mozilla firefox
  • medium trust
  • orcas
  • compileroptions
  • warninglevel
  • zyb
  • mobile phones
  • online backup service
  • online services
  • textarea
  • maxlength
  • limit input length
  • custom server control
  • internal error 2739
  • adobe cs3
  • adobe customer support
  • solution
  • error code 0x80004005
  • 500.19 internal server error
  • meta tags
  • keywords meta tag
  • meta
  • description meta tag
  • fake page rank domains
  • scammers
  • google page rank technology
  • ebay
  • general
  • title
  • page rank