1

I have the following string: (17.591257793993833, 78.88544082641602) in Javascript

How do I use split() for the above string so that I can get the numbers separately.

This is what I have tried (I know its wrong)

var location= "(17.591257793993833, 78.88544082641602)";
var sep= location.split("("" "," "")");    
document.getElementById("TextBox1").value= sep[1];
document.getElementById("Textbox2").value=sep[2];

Suggestions please

10
  • 2
    Dude. Don't use location as your variable name. It'll redirect you right away. Use loc or something else. Commented Sep 3, 2012 at 12:26
  • What's the actual rules you want to follow? As in, if you got other strings, are they always two numbers, are the brackets always there, is there always a space after the comma? Commented Sep 3, 2012 at 12:26
  • @Amaan Only if it's in the global (window) scope - this could be in a function and it should work just fine. Commented Sep 3, 2012 at 12:27
  • @Thor84no the string is always in the same format. Commented Sep 3, 2012 at 12:28
  • @Amaan Would like to know what happens if I use location as variable name in the global scope? Where will it redirect? Commented Sep 3, 2012 at 12:29

4 Answers 4

8

Use regular expression, something as simple as following would work:

// returns and array with two elements: [17.591257793993833, 78.88544082641602]
"(17.591257793993833, 78.88544082641602)".match(/(\d+\.\d+)/g)
Sign up to request clarification or add additional context in comments.

7 Comments

var sep= location.match(/(\d+\.\d+)/g); document.getElementById("TextBox1").value= sep[0]; document.getElementById("TextBox2").value= sep[1]; But this isn't working. Where am I wrong? Its seems there are no capturing groups in the regex right?
It's something else because the script's working jsfiddle.net/8JR7j Maybe you're trying to assign values to TextBox1 and TextBox2 before they're present in the DOM? Checkout chrome console or firebug - are there any Javascript errors you see?
When I wrote: document.getElementById("TextBox1").value= location; ,it worked. I had the content of location in my textbox1. How do I check the chrome console? I am working in visual studio 2008 and it doesn't show any errors in javascript, even if there are. How do I check for errors? I am a noob in javascript
Run you page in chrome, press F12 key and you'll see console. Reload the page to see if there are any errors.
Yes, but only after clicking on a map (since the bug was in a click event handler). Get familiar with console and console.log() to quickly debug this kind of problems.
|
1

You could user Regular Expression. That would help you a lot. Together with the match function.

A possible Regexp for you might be:

/\d+.\d+/g

For more information you can start with wiki: http://en.wikipedia.org/wiki/Regular_expression

Comments

0

Use the regex [0-9]+\.[0-9]+. You can try the regex here.

In javascript you could do

var str = "(17.591257793993833, 78.88544082641602)";
str.match(/(\d+\.\d+)/g);

Check it.

Comments

0

If you want the values as numbers, i.e. typeof x == "number", you would have to use a regular expression to get the numbers out and then convert those Strings into Numbers, i.e.

var numsStrings = location.match(/(\d+.\d+)/g),
    numbers = [],
    i, len = numsStrings.length;

for (i = 0; i < len; i++) {
    numbers.push(+numsStrings[i]);
}

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.