Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Related items

Math functions in JavaScript

Selecting Random Numbers

Split Ends

How long is a piece of string?

Random Numbers & Random Events

Scrolling Text

Nested Splitting

You are here: irt.org | Articles | JavaScript | Text, String, and Number | Nested Splitting [ previous next ]

Published on: Saturday 13th September 1997 By: Martin Webb

Introduction

The previous article, Split Ends, described how to split a string into parts using a separator. For example, the string:

This is a short example

can be split using a space character to produce:

However, if we use the script described in the previous article to split the following string:

This is "just another" example

then we get:

As you can see the text "just another" is split into two.

This article will describe how to split this type of nested string. The script can then be used to interrogate search strings passed from one page to another, just like a real search engine would.

Using Two Arrays

The following script uses two different arrays splitArray[], which holds the intial string split on the " character, and splitUpArray[], which holds the string split on the " character and then on the space character. Two indicies are used, splitIndex which controls the size of the splitArray[] array, and splitUpIndex which controls the size of the splitUpArray[] array.

Two arrays are needed otherwise, if we just used one, we would overwrite and overflow existing entries when attempting to use it again for output, whilst at the same time using it as the input.

<SCRIPT LANGUAGE="JavaScript"><!--
var splitIndex = 0;
var splitArray = new Array();

var splitUpIndex = 0;
var splitUpArray = new Array();

The script can be used to create the simple string split on just the " character by invoking the intermediate splits() function with the string to be split using the text character/s. This then just makes use of the splitArray[] array and the splitIndex variable.

The no integer is set to 1 by the split() function, i.e. when using the setArry() function, populate the splitArray[] array.

array, and

function split(string,text) {
    splitIndex = 0;
    splits(string,text,1);
}

Or the script can be used to the full by invoking the splitUp() function by passing the string to be split using first the separator1 character and then the separator2 character, with the by character used to replace any separator2 characters.

Before processing the string within the splitUp() function, two rather subtle changes are made to the contents. First, occurrences of two separator1 characters one after the other, have a separator2 charcater inserted between them using the replace() function, secondly, a copy of the separator2 character is prefixed to string. This ensures that the second split using the separator2 character works correctly.

The splitUp() function first sets the no integer to 1 to split the string with the splits() function using separator1, and then to 2 when splitting the alternate entries within the splitArray[] array with the splits() function using separator2. For entries that are not split further, the replace() function is used to replace separator2 characters with the by character, which is then added to the splitUpArray[] array using the setArray() function and a no integer of 2.

function splitUp(string,separator1,separator2,by) {
    splitIndex = 0, splitUpIndex = 0; 
    string = separator2 + replace(string,separator1+separator1,separator1+separator2+separator1);
    splits(string,separator1,1);

    for (var i=0; i < splitIndex; i++) {
        if ((i/2) != (Math.floor(i/2)))
            setArray(replace(splitArray[i],separator2,by),2);
        else
            splits(splitArray[i],separator2,2);
    }
}

The splits() function is almost identical to the splits() function described in the previous article Split Ends, the only difference being that rather than populating the stringArray[] array within the function, it invokes the setArray() function to populate either the splitArray[] array or the splitUpArray[] dependent on the no integer passed.

Rather than create two different versions of the splits() function, one using the splitArray[] and another using the splitUpArray[] array, its easier to use just one function with a variable no integer.

function splits(string,text,no) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return;
    if (i == -1) {
        setArray(string,no);
        return;
    }

    setArray(string.substring(0,i),no);
    
    if (i+txtLength < strLength)
        splits(string.substring(i+txtLength,strLength),text,no);

    return;
}

The setArray() function either populates the splitArray[] or the splitUpArray[] array depending on the value in the no integer. Any empty strings passed are NOT added to the arrays.

function setArray(text,no) {
    if (text != '') {
        if (no == 1) splitArray[splitIndex++]     = text;
        else         splitUpArray[splitUpIndex++] = text;
    }
}

The replace() function was described in the previous article How long is a piece of string?.

function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

The following two functions showSplitArray() and showSplitUpArray() can be used to display the contents of one or the other arrays.

function showSplitArray() {
    for (var i=0; i < splitIndex; i++)
        document.write(splitArray[i] + ' / ');
    document.write('<BR>');
}

function showSplitUpArray() {
    for (var i=0; i < splitUpIndex; i++)
        document.write(splitUpArray[i] + ' / ');
    document.write('<BR>');
}
//--></SCRIPT>

Using the script is then fairly simple and straightforward:

<SCRIPT LANGUAGE="JavaScript"><!--
splitUp('This is "just another" example','"',' ',' ');
showSplitUpArray();
//--></SCRIPT>

We can then reuse the script to split up another string, or use the simplest version if required:

<SCRIPT LANGUAGE="JavaScript"><!--
splitUp('This is "just another" example for demonstration purposes','"',' ',' ');
showSplitUpArray();
split('This is "just another" example for demonstration purposes',' ');
showSplitArray();
split('This is "just another" example for demonstration purposes','"');
showSplitArray();
//--></SCRIPT>

Working Example

Why not try it out for yourself?

Related items

Math functions in JavaScript

Selecting Random Numbers

Split Ends

How long is a piece of string?

Random Numbers & Random Events

Scrolling Text

©2018 Martin Webb