Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to split a string in javascript [duplicate]

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 "
like image 908
user177785 Avatar asked Sep 29 '09 15:09

user177785


People also ask

How can I split a string into two JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you split a new line in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

Can you split an array JavaScript?

Splitting the Array Into Even Chunks Using slice() Method The easiest way to extract a chunk of an array, or rather, to slice it up, is the slice() method: slice(start, end) - Returns a part of the invoked array, between the start and end indices.


1 Answers

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 */
like image 86
Sampson Avatar answered Oct 04 '22 01:10

Sampson