JavaScript
  Home arrow JavaScript arrow Page 4 - Stringing Things Along
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Stringing Things Along
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2003-01-22

    Table of Contents:
  • Stringing Things Along
  • Elementary School
  • When Size Does Matter
  • Slice And Dice
  • Building Character
  • Search And Destroy
  • Bigger, Bolder, Better

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Stringing Things Along - Slice And Dice
    (Page 4 of 7 )

    Next up, the substring() method. As the name implies, this is the function that allows you to slice and dice strings into smaller strings. Here's what it looks like:

    String.substring(start, end)


    where "start" is the position to begin slicing at, and "end" is the position to end at (note that in JavaScript, the first character of a string is at position 0).

    Here's an example which demonstrates how this works:
    <script language="Javascript">
    // set string
    var str = "The horse munched pink flowers, waiting for the elephant to
    make its presence felt.";
    // returns "pink flowers"
    alert(str.substring(18, 30));
    </script>


    You can use this function to split a string into smaller chunks of a fixed size,

    <script language="Javascript">
    // set string
    var str = "The horse munched pink flowers, waiting for the elephant to
    make its presence felt.";
    // chunk size
    var size = 9;
    // counter
    var count = 0;
    /* returns 
    The horse
    munched 
    pink flow
    ers, wait
    ing for t
    he elepha
    nt to mak
    e its pre
    sence fel
    t.
    */
    while ((size*count) < str.length)
    {
    temp = str.substring(size*count, (size*count)+size);
    count++;
    document.write(temp + "\r\n");
    }
    </script>


    You can also use the substring() method to extract a particular character from a string,

    <script language="Javascript">
    // set string
    var str = "The horse munched pink flowers, waiting for the elephant to
    make its presence felt.";
    // returns "p"
    alert(str.substring(18,19));
    </script>/pre>


    Similar, though not identical, is the substr() method, which returns a substring, given the start position and the number of characters to be extracted. This method differs from the substring() method discussed above in that its second argument is not the end position, but the number of characters to be returned - as illustrated below:

    <script language="Javascript">
    // set string
    var str = "Batman and Robin";
    // returns "Batman"
    alert(str.substr(0,6)); 
    </script>

    More JavaScript Articles
    More By Harish Kamath, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript

    Iron Speed



    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway