PDA

View Full Version : Split method/function/process?



bluewalrus
09-16-2009, 08:32 PM
I'm trying to split a string up but am failing. The string glatlng2 contains "(22.2342, -123.23423)". I tried changing the name to a simple string and it produced the same error message which was "TypeError: glatlng2.split is not a function". The value of glatlng2 will always be changing but will always be in the same format (any number of numbers, any number of numbers), so I thought this would give me the 2 sets of numbers in 2 variables. Thanks for any help you can offer.


var mySplitResult = glatlng2.split("(");
var mySplitResult2 = mySplitResult[0];
var mySplitResult3 = mySplitResult2.split(",");
var lat2avg = mySplitResult3[0];
var mySplitResult4 = mySplitResult3[1];
var mySplitResult5 = mySplitResult4.split(")");
var lng2avg = mySplitResult5[0];

MJH
09-16-2009, 10:47 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>None</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var glatlng2 = "(22.2342, -123.23423)";

function init(){

var latLongAvgs = glatlng2;
latLongAvgs = latLongAvgs.replace(/[()]/g, "").split(",");
var lat2avg = latLongAvgs[0];
alert(lat2avg);
var lng2avg = latLongAvgs[1];
alert(lng2avg);
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

</script>
</head>
<body>
<form action="" method="post">


</form>
</body>
</html>

bluewalrus
09-17-2009, 01:20 AM
Could you explain the difference in statements? I understand the replace function but I'm missing why you have to declare a string to a string "var latLongAvgs = glatlng2;" and do splits have to be in their own function? I haven't had a chance yet to test this, switching over database, but thanks for the code thus far.