Category: Javascript String

JavaScript Split Method: A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values; each of the substrings is an element in the list.

 

For instance, the following string has three elements separated by commas:

“First element, Second element, Third element”

 

The string object provides the JavaScript Split Method, which you can use to split a string into elements at a specified delimiter. These elements are then placed in an array, and that array is returned by the method.

 

For instance, consider the following example of JavaScript Split Method:

var thisVar = “First element,Second element,Third element”;

var anotherVar = thisVar.split(“,”);

anotherVar is now an array containing three elements.

The following task illustrates JavaScript Split Method this by splitting a string containing a list into its component elements and then outputting those elements from the resulting array:

 

1. Open a new HTML document in your preferred HTML or text editor.

2. Create the body of the document with opening and closing body tags:

<body>

</body>

3. Insert a script block in the body of the document:

<script language=”JavaScript”>

<!--

// -->

</script>

4. Create a variable named myVariable and assign a comma separated text string to it:

var myVariable = “a,b,c,d”;

5. Use the JavaScript Split Method to split the string at the commas and assign the resulting array to the variable stringArray:

var stringArray = myVariable.split(“,”);

6. Use the document.write method to output the elements of the array so that the final script looks like

 

<body>

<script language=”JavaScript”>

<!--

var myVariable = “a,b,c,d”;

var stringArray = myVariable.split(“,”);

document.write(stringArray[0]);

document.write(stringArray[1]);

document.write(stringArray[2]);

document.write(stringArray[3]);

// -->

</script>

</body>

 

7. Save the file and close it.

8. Open the file in a browser, and you should see the text “abcd”,



Dinesh ThakurDinesh Thakur is a Columinist and designer with strong passion and founder of Computer Notes. if you have any ideas or any request please get @me on Google+
linkedin FaceBook Twitter Google Plus