Home arrow JavaScript arrow Page 4 - Stringing Things Along

Slice And Dice - JavaScript

Think JavaScript's only good for image swaps and tickertapes?Think again - the language comes with a powerful String() objectdesigned to help you quickly and efficiently perform string manipulationtasks in the client. This article explains how, with illustrations andcode samples.

TABLE OF CONTENTS:
  1. Stringing Things Along
  2. Elementary School
  3. When Size Does Matter
  4. Slice And Dice
  5. Building Character
  6. Search And Destroy
  7. Bigger, Bolder, Better
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 10
January 22, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVASCRIPT ARTICLES

- Javascript for Beginners: An Introduction
- Introduction to JavaScript
- Adding Elements to a Tree with TreeView jQue...
- Using the Persist Argument in a TreeView jQu...
- Using Unique and Toggle in a TreeView jQuery...
- Using Event Delegation for Mouseover Events ...
- Using the Animate Option in a Treeview jQuer...
- Using HTML Lists with Event Delegation in Ja...
- Opened and Closed Branches on a TreeView jQu...
- Mouseover Events and Event Delegation in Jav...
- Creating a TreeView JQuery Hierarchical Navi...
- Event Delegation in JavaScript
- A Look at the New YUI Carousel Control
- Working with Draggable Elements and Transpar...
- Displaying Pinned Handles with Resizable Con...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: