5

Possible Duplicate:
How do I split this string with JavaScript?

how to split a string in javascript?

example str = "this is part 1 one wall this is part 2 "
now I want to split the str in 2 parts separated by word wall

so I want output to be:

st1 ="this is part 1 "
st2 ="this is part 2 "
Community
  • 1
  • 1
user177785
  • 2,249
  • 6
  • 22
  • 16

5 Answers5

16
var str1 = "When I say wall I want to split";
var chunks = str1.split("wall");

alert(chunks[0]); /* When I say */
alert(chunks[1]); /* I want to split */
Sampson
  • 265,109
  • 74
  • 539
  • 565
2

Just for the sake of completeness, I'll add a regular expressions answer:

var splits = str.split(/wall /);
alert("'"+splits[0]+"'"); //'this is part 1 '
alert("'"+splits[1]+"'"); //'this is part 2 '
Brandon Belvin
  • 445
  • 7
  • 14
1
var parts = str.split('wall ');

var st1 = parts[0];
var st2 = parts[1];
Alex Barrett
  • 16,175
  • 3
  • 52
  • 51
1

String.split

knittl
  • 246,190
  • 53
  • 318
  • 364
0
var s = str.split("wall");
var st1 = s[0];
var st2 = s[1];
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283