Solved

javascript - split strings

Posted on 2011-09-18
3
255 Views
Last Modified: 2012-08-13
I want to split a string according to this example:
"a|b||c||d|e"
into
"a"
"b"
""
"c"
""
"d"
"e"

If possible, please provide some sample code. Thanks!

0
Comment
Question by:dshrenik
[X]
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
3 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 36557386
Hi,

Use split function

.split("|");

<script type="text/javascript">

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

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

</script>

It will give you an string array.


/peter

0
 
LVL 22

Accepted Solution

by:
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 />");
}

</script>

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
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 : http://jsfiddle.net/UekVP/
<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 />");
    }

</script>

Open in new window

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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

636 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