Solved

javascript - split strings

Posted on 2011-09-18
3
240 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
  • 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
insert a falling animation of heart  in php 4 23
Simple Total function not working 8 32
replace link with jQuery 4 15
Else condition 9 19
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

803 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