Thursday, June 18, 2009

String Split using JavaScript

Found this helpful when I'm trying to modify a script that I didn't do...hehehe

Split a string in JavaScript using the following method:

var stringToSplit = "string-to-split";
var splitArray = stringToSplit.split("-"); //splits the string when the system encounters "-", the delimeter
In a function:
<SCRIPT language="JavaScript">
<!--
function split_string() {
var stringToSplit = "string-to-split";
var splitArray = stringToSplit.split("-");
alert(splitArray[0] + "\n" + splitArray[1] + "\n" + splitArray[2]);
}
//-->
</SCRIPT>
<FORM>
<INPUT TYPE="button" onClick="split_string()" value="Split string-to-split!" />
</FORM>

No comments: