-
Split string
hello,
does anyone know how to split a string?
so far I have the following:
Code:
var string = "Hello my name is - Bob";
alert( sring.split('-', 2) );
however, I am not 100% sure if i am doing it correct.
I am trying to get the bob part of the string only.
but it displays Hello my name is , bob
Does anyone know what I am doing wrong?
-
Re: Split string
You're splitting properly. Remember that it returns an array.
This is very, very basic JavaScript. I suggest you go to W3Schools' JavaScript section and brush up a bit.
-
Re: Split string
Here is the code to split a string in javascript..Hope it will work for you
string.split(separator, limit)
An example for your help goes like this :
<script type="text/javascript">
var str="How are you doing today?";
document.write(str.split() + "<br />");
document.write(str.split(" ") + "<br />");
document.write(str.split("") + "<br />");
document.write(str.split(" ",3));
</script>