Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Doing the splits! ->>help on split


Message #1 by "Rajesh" <rajesh.singh@t...> on Thu, 21 Dec 2000 09:02:32 +0530
Hello,
This is the complete help on Split function().
See that have specified which version to use.

/***************************
Splits a String object into an array of strings by separating the string
into substrings.

Method of String
Implemented in JavaScript 1.1, NES 2.0
Syntax

split([separator][, limit])

Parameters


separator
 Specifies the character to use for separating the string. The separator is
treated as a string. If separator is omitted, the array returned contains
one element consisting of the entire string.
limit
 Integer specifying a limit on the number of splits to be found.
Description

The split method returns the new array.When found, separator is removed from
the string and the substrings are returned in an array. If separator is
omitted, the array contains one element consisting of the entire string.In
JavaScript 1.2, split has the following additions:


·It can take a regular expression argument, as well as a fixed string, by
which to split the object string. If separator is a regular expression, any
included parenthesis cause submatches to be included in the returned array.

·It can take a limit count so that the resulting array does not include
trailing empty elements.

·If you specify LANGUAGE="JavaScript1.2" in the SCRIPT tag, string.split("
") splits on any run of 1 or more white space characters including spaces,
tabs, line feeds, and carriage returns. For this behavior,
LANGUAGE="JavaScript1.2" must be specified in the <SCRIPT> tag.

Examples

Example 1. The following example defines a function that splits a string
into an array of strings using the specified separator. After splitting the
string, the function displays messages indicating the original string
(before the split), the separator used, the number of elements in the array,
and the individual array elements.

function splitString (stringToSplit,separator) {
   arrayOfStrings = stringToSplit.split(separator)
   document.write ('<P>The original string is: "' + stringToSplit + '"')

   document.write ('<BR>The separator is: "' + separator + '"')
   document.write ("<BR>The array has " + arrayOfStrings.length + "
elements: ")

  for (var i=0; i < arrayOfStrings.length; i++) {
      document.write (arrayOfStrings[i] + " / ")
   }
}

var tempestString="Oh brave new world that has such people in it."
var monthString="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"

var space=" "
var comma=","

splitString(tempestString,space)

splitString(tempestString)
splitString(monthString,comma)

This example produces the following output:

The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such /
people / in / it. /

The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it. /

The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug /
Sep / Oct / Nov / Dec /

Example 2. Consider the following script:

<SCRIPT LANGUAGE="JavaScript1.2">
str="She sells    seashells \nby   the\n seashore"
document.write(str + "<BR>")
a=str.split(" ")
document.write(a)
</SCRIPT>

Using LANGUAGE="JavaScript1.2", this script produces

"She", "sells", "seashells", "by", "the", "seashore"

Without LANGUAGE="JavaScript1.2", this script splits only on single space
characters, producing

"She", "sells", , , , "seashells", "by", , , "the", "seashore"

Example 3. In the following example, split looks for 0 or more spaces
followed by a semicolon followed by 0 or more spaces and, when found,
removes the spaces from the string. nameList is the array returned as a
result of split.

<SCRIPT>
names = "Harry  Trump  ;Fred Barney; Helen   Rigby ;  Bill Abel ;Chris Hand
";
document.write (names + "<BR>" + "<BR>");
re = /\s*;\s*/;
nameList = names.split (re);
document.write(nameList);
</SCRIPT>

This prints two lines; the first line prints the original string, and the
second line prints the resulting array.Harry Trump ;Fred Barney; Helen Rigby
; Bill Abel ;Chris Hand
Harry Trump,Fred Barney,Helen Rigby,Bill Abel,Chris HandExample 4. In the
following example, split looks for 0 or more spaces in a string and returns
the first 3 splits that it finds.

<SCRIPT LANGUAGE="JavaScript1.2">
myVar = "  Hello World. How are you doing?    ";
splits = myVar.split(" ", 3);
document.write(splits)
</SCRIPT>

This script displays the following:

["Hello", "World.", "How"]



----- Original Message -----
From: Luke Chapman <luke@p...>
To: javascript <javascript@p...>
Sent: Wednesday, December 20, 2000 8:38 PM
Subject: [javascript] Doing the splits!


> Can anyone help?
> I have the following code:
>
>
>
>
> function DoThis(item) {
>     var more=item.split(".");
>     if (more[1]) {
>     alert(more[0]+more[1]);
>     }
>     else {
>     alert(more[0]);
>     }
> }
>
> DoThis(form.price.value/10000);
>
>
>
>
> The problem is, I keep on getting an error of "item.split is not a
> function."
> Cheers
> Luke
>
>

--- 
NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS?  Is FREE okay?
Visit EarthWeb for the latest in IT Management, Software Development, 
Web Development, Networking & Communications, and Hardware & Systems.  
Click on http://www.earthweb.com for FREE articles, tutorials,
and discussions from the experts.
---
You are currently subscribed to javascript as: $subst('Recip.EmailAddr')
To unsubscribe send a blank email to leave-javascript-$subst('Recip.MemberIDChar')@p2p.wrox.com

  Return to Index