Erik Soderstrom
Erik Soderstrom

Reputation: 313

Extract two lines of digits from a String

I have a gps module that sends strings of data to my Android app.

For example a string I get can look like this:

http:/maps.google.com/maps?q=59.0000000,16.0000000

How can i extract the numbers into two different strings.

Thanks on forehand

Upvotes: 1

Views: 107

Answers (4)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136002

try this

Matcher m = Pattern.compile("\\d+\\.\\d+").matcher(str);
m.find();
String n1 = m.group();
m.find();
String n2 = m.group();

if the format is fixed then we can make it simpler

String[] str = "http:/maps.google.com/maps?q=59.0000000,16.0000000".replaceAll("\\D+(.+)", "$1").split(",");
String n1 = str[0];
String n2 = str[1];

Upvotes: 0

g00dy
g00dy

Reputation: 6778

Do the following:

String address = "http:/maps.google.com/maps?q=59.0000000,16.0000000";
String[] splited = address .split("=");
splited[0]; // this will contain "http:/maps.google.com/maps?q"
splited[1]; // this will contain "59.0000000,16.0000000"

String[] latlong = splited[1].split(",");

latlog[0]; // Should contain 59.0000000
latlog[1]; // Should contain 16.0000000

Cheers :-)

Upvotes: 0

Rahul
Rahul

Reputation: 135

split() method of String class returns array of string so you can use following code:

String s = req.getParameter("q'); String[] arr = s.split(",");

String num1 = arr[0]; String num2 = arr[1];

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

Uri uri=Uri.parse(yourString);
String result=uri.getQueryParameter("q");

Then split your result with ,,which gives you a array of strings (contains your numbers as strings).

Upvotes: 5

Related Questions