javascript - split strings

Posted on 2011-09-18
Last Modified: 2012-08-13
I want to split a string according to this example:

If possible, please provide some sample code. Thanks!

Question by:dshrenik
  • 2
LVL 22

Expert Comment

ID: 36557386

Use split function


<script type="text/javascript">

var str="a|b||c||d|e";

document.write(str.split("|") + "<br />");


It will give you an string array.


LVL 22

Accepted Solution

pivar earned 400 total points
ID: 36557406
If you want to see split surrounded by " on each line try

<script type="text/javascript">

var str="a|b||c||d|e";

var arr = str.split("|") ;
for(var i=0; i<arr.length; i++) {
	var value = arr[i];
	document.write("&quot;" + value + "&quot;<br />");


Open in new window

LVL 82

Assisted Solution

leakim971 earned 100 total points
ID: 36557409
pivar is right, use the split function to get an array of string.
but use a loop to display each string of the array :
<script type="text/javascript">

    var str = "a|b||c||d|e";
    str = str.split("|");

    for(var i=0;i<str.length;i++) { 
        document.write(str[i] + "<br />");


Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access RV042 GUI / Browser Issues 25 52
Json Schema 8 32
Filktering Alphabetically 8 32
JS Event Does not Trigger From File 2 29
In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question