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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Suggested Courses

752 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