-1

I have a string that returns from google map api. it's about the latitude and longitude, that's how it looks (25.229355, 55.302734)

I want to remove ( , ) , and the space in order to use these two numbers.

I want these two numbers in an array for example or alert each one of them.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
hasan.alkhatib
  • 1,509
  • 3
  • 14
  • 28
  • 2
    Ok... So what did you try to do? – Naftali Aug 14 '13 at 13:50
  • please tell what you are expecting form this string. I mean do you want both numbers in an array, or what? – Manish Kumar Aug 14 '13 at 13:51
  • The google map API returns a string of value? Isn't it an array? In that case Array[0] and Array[1] would do. How do you get theses numbers? I could tell you to replace() or split() but i'm pretty sure thats not the correct way of doing this – Bene Aug 14 '13 at 13:54
  • http://stackoverflow.com/questions/987382/javascript-extracting-number-from-string or http://stackoverflow.com/questions/7033334/how-to-extract-number-from-a-string-in-javascript – doctorlove Aug 14 '13 at 13:54
  • actually I tried to use a for loop to check each character and replace it with nothing if it's one from ( ) space, – hasan.alkhatib Aug 14 '13 at 13:58
  • it works, thanks. The problem was that I was trying to print an object, so when I used a string and concatenated it with the object it worked. – hasan.alkhatib Aug 14 '13 at 14:11

6 Answers6

2

Parse with a regular expression. This is the simplest case:

var input = "(25.229355, 55.302734)";

var arr = input.match(/[0-9\.]+/g); alert(arr);

var lat = arr[0];
var long = arr[1];
fred02138
  • 3,323
  • 1
  • 14
  • 17
  • match() is a string method, if it says "no method match" then you are not dealing with a string – Bene Aug 14 '13 at 14:04
  • it works, thanks. The problem was that I was trying to print an object, so when I used a string and concatenated it with the object it worked. – hasan.alkhatib Aug 14 '13 at 14:10
  • If it was an object why don't you get the numbers directly rather than converting to string and parsing it? – az_ Aug 14 '13 at 14:25
2
str = "(25.229355, 55.302734)";


str =str.replace("(",""); 
str =str.replace(")",""); 
str =str.replace(",",""); 

var n=str.split(" "); 

console.log(n);
Bhavin Rana
  • 1,554
  • 4
  • 20
  • 40
  • it works, thanks the problem was that I was trying to print an object, so when I used a string and concatenated it with the object it worked. – hasan.alkhatib Aug 14 '13 at 14:07
2

Alright, usually if you are trying to extract things from a string, you need to figure out:

  1. what you DONT want in the string
  2. How you can distinguish between the things you DO want

In your case, what you don't want is parentheses and spaces. In JS, there is a function you can call on strings called replace(substr,replacement) which will do exactly what it sounds like.

Lets assume your paired string is called latLong. Once you've removed what you don't want:

latLong.replace(/[()\s]/g, "");

then you can split up the string based on the delimiter between them (the comma) using the split() method

var latLongArray = latLong.split(",");

The split() method will divide a string up into an array of substrings, separated by the argument you pass it. So at the end of this, your latLongArray will be an array containing 2 elements (the latitude and longitude)!

0

try it

 var x= '(25.229355, 55.302734)'.replace(/[^A-Za-z\s]+/g, '');
  // x is : `25.229355 55.302734`
  var n=x.split(" "); 
alert("Long is" + n[0] + " lat is "+ n[1] )
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

You keep on saying it's an object, in which case it makes no sense to parse the string. If you really need a string output, just get the numbers directly from the object and concatenate it.

obj[0]+","+obj[1]
az_
  • 1,493
  • 1
  • 16
  • 24
0

How about...

var coordinatesString, coordinates, longitude, latitude; 

coordinatesString = '(25.229355, 55.302734)';
coordinates = coordinatesString.substring(1, coordinatesString.length - 1).split(', ');
longitude = coordinates[0];
latitude = coordinates[1];
Bill Criswell
  • 32,161
  • 7
  • 75
  • 66