I want to send a location to Google Geocoding API, therefore I want to replace any space or comma in the text (as it can be received) with +.
For example, all those samples should return Glentworth+Ireland :
Glentworth Ireland
Glentworth,Ireland
Glentworth, Ireland
I tried with: place.replaceAll("/\\\\\\\\b ,/\\\\\\\\b|[ ,]", "+")
. But I still got in the latest case Glentworth++Ireland (where it's should be one plus only).
So the question is how can I replace " ," with plus (and just if <space>,
not exist exact to replace the space/comma with plus) ?
(Or in other words, how can I be sure the place is ready to send as request to the API).
PS Of course, I can do it with to replaces, but not want!
Simply you can do it with two replaces: at first you replace any "," to " ", and next you replace all spaces to one plus. If you want to do it with one replace, you can use "[\\,\\s]\\s*"
If you want to replace any sequence of commas and spaces in your string with a single plus sign, you can do this:
place.replaceAll("[, ]+", "+")
[, ]
is a character class containing comma and space, that means it will match either a comma or a space.
+
means "the preceding item one or more times".
So [, ]+
means "one or more occurrences of commas and/or spaces".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.