Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

javascript - split strings

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
dshrenik
Asked:
dshrenik
  • 2
2 Solutions
 
pivarCommented:
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
 
pivarCommented:
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
 
leakim971PluritechnicianCommented:
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 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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now