Solved

javascript - split strings

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now