-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.

6
  • 2
    Ok... So what did you try to do? Commented Aug 14, 2013 at 13:50
  • please tell what you are expecting form this string. I mean do you want both numbers in an array, or what? Commented Aug 14, 2013 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 Commented Aug 14, 2013 at 13:54
  • stackoverflow.com/questions/987382/… or stackoverflow.com/questions/7033334/… Commented Aug 14, 2013 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, Commented Aug 14, 2013 at 13:58

6 Answers 6

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];
Sign up to request clarification or add additional context in comments.

3 Comments

match() is a string method, if it says "no method match" then you are not dealing with a string
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.
If it was an object why don't you get the numbers directly rather than converting to string and parsing it?
2
str = "(25.229355, 55.302734)";


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

var n=str.split(" "); 

console.log(n);

1 Comment

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.
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)!

Comments

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] )

Comments

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]

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.