Results 1 to 3 of 3

Thread: Split method/function/process?

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Split method/function/process?

    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.

    Code:
    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];

  2. #2
    Join Date
    Nov 2008
    Posts
    40
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default

    Code:
    <!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>

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •