Getting rid of half a String in Android
up vote
1
down vote
favorite
Need help with getting rid of half of a string in android studio. The string is:
final String strOrigin = String.valueOf(origin).trim();
The value that is returned is;
"Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}"
I want to be left with only the numbers of that, in the String. I have tried;
strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
But it isn't working. Any help would be appreciated.
java
add a comment |
up vote
1
down vote
favorite
Need help with getting rid of half of a string in android studio. The string is:
final String strOrigin = String.valueOf(origin).trim();
The value that is returned is;
"Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}"
I want to be left with only the numbers of that, in the String. I have tried;
strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
But it isn't working. Any help would be appreciated.
java
strOrigin.replaceAll("\D+","");
if you want to use regrex to get just digits of a string.
– SnakeyHips
Nov 22 at 14:27
Isn't there an option to just get the coordinates? Something likefinal String origin = origin.getCoordinates().toString();
?
– PPartisan
Nov 22 at 14:30
What isorigin
?
– TheWanderer
Nov 22 at 14:35
@TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
– Alexander Mcmichael
Nov 22 at 14:50
That's not very helpful. What is the Origin object and where is it from?
– TheWanderer
Nov 22 at 14:51
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Need help with getting rid of half of a string in android studio. The string is:
final String strOrigin = String.valueOf(origin).trim();
The value that is returned is;
"Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}"
I want to be left with only the numbers of that, in the String. I have tried;
strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
But it isn't working. Any help would be appreciated.
java
Need help with getting rid of half of a string in android studio. The string is:
final String strOrigin = String.valueOf(origin).trim();
The value that is returned is;
"Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}"
I want to be left with only the numbers of that, in the String. I have tried;
strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
But it isn't working. Any help would be appreciated.
java
java
edited Nov 22 at 15:21
Milo Bem
792318
792318
asked Nov 22 at 14:19
Alexander Mcmichael
267
267
strOrigin.replaceAll("\D+","");
if you want to use regrex to get just digits of a string.
– SnakeyHips
Nov 22 at 14:27
Isn't there an option to just get the coordinates? Something likefinal String origin = origin.getCoordinates().toString();
?
– PPartisan
Nov 22 at 14:30
What isorigin
?
– TheWanderer
Nov 22 at 14:35
@TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
– Alexander Mcmichael
Nov 22 at 14:50
That's not very helpful. What is the Origin object and where is it from?
– TheWanderer
Nov 22 at 14:51
add a comment |
strOrigin.replaceAll("\D+","");
if you want to use regrex to get just digits of a string.
– SnakeyHips
Nov 22 at 14:27
Isn't there an option to just get the coordinates? Something likefinal String origin = origin.getCoordinates().toString();
?
– PPartisan
Nov 22 at 14:30
What isorigin
?
– TheWanderer
Nov 22 at 14:35
@TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
– Alexander Mcmichael
Nov 22 at 14:50
That's not very helpful. What is the Origin object and where is it from?
– TheWanderer
Nov 22 at 14:51
strOrigin.replaceAll("\D+","");
if you want to use regrex to get just digits of a string.– SnakeyHips
Nov 22 at 14:27
strOrigin.replaceAll("\D+","");
if you want to use regrex to get just digits of a string.– SnakeyHips
Nov 22 at 14:27
Isn't there an option to just get the coordinates? Something like
final String origin = origin.getCoordinates().toString();
?– PPartisan
Nov 22 at 14:30
Isn't there an option to just get the coordinates? Something like
final String origin = origin.getCoordinates().toString();
?– PPartisan
Nov 22 at 14:30
What is
origin
?– TheWanderer
Nov 22 at 14:35
What is
origin
?– TheWanderer
Nov 22 at 14:35
@TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
– Alexander Mcmichael
Nov 22 at 14:50
@TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
– Alexander Mcmichael
Nov 22 at 14:50
That's not very helpful. What is the Origin object and where is it from?
– TheWanderer
Nov 22 at 14:51
That's not very helpful. What is the Origin object and where is it from?
– TheWanderer
Nov 22 at 14:51
add a comment |
5 Answers
5
active
oldest
votes
up vote
4
down vote
accepted
In Java strings are immutable. You must assign the result to a new string:
String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
Thank you, works nicely!
– Alexander Mcmichael
Nov 22 at 14:42
add a comment |
up vote
2
down vote
Maybe you forgot that replace()
returns the result and you must assign it to a string:
String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
String str = strOrigin
.replace("Point{type=Point, bbox=null, coordinates=", "")
.replace("}","");
System.out.println(str);
will print:
[27.993726079654873,-26.14686805145815]
add a comment |
up vote
2
down vote
Make your life simpler by just getting the coordinates:
final String strOigin = origin.coordinates().stream()
.map(String::valueOf)
.collect(Collectors.joining(",","{","}"));
Or, if you're stuck on java 7:
final String strOigin = String.format(
"{%s,%s}",
String.valueOf(origin.latitude()),
String.valueOf(origin.longitude())
);
1
Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
– TheWanderer
Nov 22 at 14:34
Alright, I've added an answer for Java 7
– PPartisan
Nov 22 at 14:38
add a comment |
up vote
1
down vote
This should do the trick
String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
will print:
27.993726079654873,-26.14686805145815
Than you can cast it to double
or latlong
format
add a comment |
up vote
1
down vote
Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:
String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
In Java strings are immutable. You must assign the result to a new string:
String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
Thank you, works nicely!
– Alexander Mcmichael
Nov 22 at 14:42
add a comment |
up vote
4
down vote
accepted
In Java strings are immutable. You must assign the result to a new string:
String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
Thank you, works nicely!
– Alexander Mcmichael
Nov 22 at 14:42
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
In Java strings are immutable. You must assign the result to a new string:
String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
In Java strings are immutable. You must assign the result to a new string:
String strResult = strOrigin.replace("Point{type=Point, bbox=null, coordinates=", "");
edited Nov 22 at 14:28
ZUNJAE
6341618
6341618
answered Nov 22 at 14:22
Psytho
1,8711819
1,8711819
Thank you, works nicely!
– Alexander Mcmichael
Nov 22 at 14:42
add a comment |
Thank you, works nicely!
– Alexander Mcmichael
Nov 22 at 14:42
Thank you, works nicely!
– Alexander Mcmichael
Nov 22 at 14:42
Thank you, works nicely!
– Alexander Mcmichael
Nov 22 at 14:42
add a comment |
up vote
2
down vote
Maybe you forgot that replace()
returns the result and you must assign it to a string:
String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
String str = strOrigin
.replace("Point{type=Point, bbox=null, coordinates=", "")
.replace("}","");
System.out.println(str);
will print:
[27.993726079654873,-26.14686805145815]
add a comment |
up vote
2
down vote
Maybe you forgot that replace()
returns the result and you must assign it to a string:
String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
String str = strOrigin
.replace("Point{type=Point, bbox=null, coordinates=", "")
.replace("}","");
System.out.println(str);
will print:
[27.993726079654873,-26.14686805145815]
add a comment |
up vote
2
down vote
up vote
2
down vote
Maybe you forgot that replace()
returns the result and you must assign it to a string:
String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
String str = strOrigin
.replace("Point{type=Point, bbox=null, coordinates=", "")
.replace("}","");
System.out.println(str);
will print:
[27.993726079654873,-26.14686805145815]
Maybe you forgot that replace()
returns the result and you must assign it to a string:
String strOrigin = "Point{type=Point, bbox=null, coordinates=[27.993726079654873,-26.14686805145815]}";
String str = strOrigin
.replace("Point{type=Point, bbox=null, coordinates=", "")
.replace("}","");
System.out.println(str);
will print:
[27.993726079654873,-26.14686805145815]
answered Nov 22 at 14:28
forpas
6,0881218
6,0881218
add a comment |
add a comment |
up vote
2
down vote
Make your life simpler by just getting the coordinates:
final String strOigin = origin.coordinates().stream()
.map(String::valueOf)
.collect(Collectors.joining(",","{","}"));
Or, if you're stuck on java 7:
final String strOigin = String.format(
"{%s,%s}",
String.valueOf(origin.latitude()),
String.valueOf(origin.longitude())
);
1
Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
– TheWanderer
Nov 22 at 14:34
Alright, I've added an answer for Java 7
– PPartisan
Nov 22 at 14:38
add a comment |
up vote
2
down vote
Make your life simpler by just getting the coordinates:
final String strOigin = origin.coordinates().stream()
.map(String::valueOf)
.collect(Collectors.joining(",","{","}"));
Or, if you're stuck on java 7:
final String strOigin = String.format(
"{%s,%s}",
String.valueOf(origin.latitude()),
String.valueOf(origin.longitude())
);
1
Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
– TheWanderer
Nov 22 at 14:34
Alright, I've added an answer for Java 7
– PPartisan
Nov 22 at 14:38
add a comment |
up vote
2
down vote
up vote
2
down vote
Make your life simpler by just getting the coordinates:
final String strOigin = origin.coordinates().stream()
.map(String::valueOf)
.collect(Collectors.joining(",","{","}"));
Or, if you're stuck on java 7:
final String strOigin = String.format(
"{%s,%s}",
String.valueOf(origin.latitude()),
String.valueOf(origin.longitude())
);
Make your life simpler by just getting the coordinates:
final String strOigin = origin.coordinates().stream()
.map(String::valueOf)
.collect(Collectors.joining(",","{","}"));
Or, if you're stuck on java 7:
final String strOigin = String.format(
"{%s,%s}",
String.valueOf(origin.latitude()),
String.valueOf(origin.longitude())
);
edited Nov 22 at 14:37
answered Nov 22 at 14:34
PPartisan
4,62541636
4,62541636
1
Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
– TheWanderer
Nov 22 at 14:34
Alright, I've added an answer for Java 7
– PPartisan
Nov 22 at 14:38
add a comment |
1
Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
– TheWanderer
Nov 22 at 14:34
Alright, I've added an answer for Java 7
– PPartisan
Nov 22 at 14:38
1
1
Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
– TheWanderer
Nov 22 at 14:34
Stream is a Java 8 feature. Most Android apps are using Java 7, unless they're only targeting Nougat and later.
– TheWanderer
Nov 22 at 14:34
Alright, I've added an answer for Java 7
– PPartisan
Nov 22 at 14:38
Alright, I've added an answer for Java 7
– PPartisan
Nov 22 at 14:38
add a comment |
up vote
1
down vote
This should do the trick
String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
will print:
27.993726079654873,-26.14686805145815
Than you can cast it to double
or latlong
format
add a comment |
up vote
1
down vote
This should do the trick
String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
will print:
27.993726079654873,-26.14686805145815
Than you can cast it to double
or latlong
format
add a comment |
up vote
1
down vote
up vote
1
down vote
This should do the trick
String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
will print:
27.993726079654873,-26.14686805145815
Than you can cast it to double
or latlong
format
This should do the trick
String s="blabla coordinates=[27.993726079654873,-26.14686805145815] ";
String requiredString = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
will print:
27.993726079654873,-26.14686805145815
Than you can cast it to double
or latlong
format
answered Nov 22 at 14:29
Hossam Hassan
1401116
1401116
add a comment |
add a comment |
up vote
1
down vote
Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:
String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
add a comment |
up vote
1
down vote
Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:
String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
add a comment |
up vote
1
down vote
up vote
1
down vote
Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:
String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Have a look at regular expressions, they allow you to define more flexible search patterns. In your example you only find the coordinates if the rest of the string matches the pattern exactly, but if you happen to get some other value of bbox, or even extra space it will not work. This will always match everything between a pair of square brackets:
String c = origin.replaceAll(".+?(\[.+?\]).+?", "$1");
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
answered Nov 22 at 14:54
Milo Bem
792318
792318
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432953%2fgetting-rid-of-half-a-string-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
strOrigin.replaceAll("\D+","");
if you want to use regrex to get just digits of a string.– SnakeyHips
Nov 22 at 14:27
Isn't there an option to just get the coordinates? Something like
final String origin = origin.getCoordinates().toString();
?– PPartisan
Nov 22 at 14:30
What is
origin
?– TheWanderer
Nov 22 at 14:35
@TheWanderer Origin is the variable for a Mapbox co-ordinate, and i want to save those co-ordinates in a database.
– Alexander Mcmichael
Nov 22 at 14:50
That's not very helpful. What is the Origin object and where is it from?
– TheWanderer
Nov 22 at 14:51