0

How can I get a parse String of city name without whitespaces (on the start and at the end) and with "%20" instead of " " (which contains some city names) in android.

For example I would like to parse:

" Oklahoma " -> "Oklahoma"

or

"    Oklahoma    City" -> "Oklahoma%20City"
y07k2
  • 1,898
  • 4
  • 20
  • 36
  • Possible duplicate of [Java how to replace 2 or more spaces with single space in string and delete leading spaces only](http://stackoverflow.com/questions/2932392/java-how-to-replace-2-or-more-spaces-with-single-space-in-string-and-delete-lead) – Selvin Feb 29 '16 at 16:47

1 Answers1

1
Uri.encode(" Oklahoma  City".trim())  

Output :

Oklahoma%20%City

Leonid Veremchuk
  • 1,952
  • 15
  • 27
  • "Oklahoma [space][space]City" Output: `Oklahoma%20%20City` ... should be `" Oklahoma City".replaceAll("\\s+", " ").trim()` – Selvin Feb 29 '16 at 16:46
  • For me Leonid's code works fine. I get from geocode API what I need! But thanks you too. – y07k2 Feb 29 '16 at 17:05