0

So I want to make my string like "abc 123 def" to be "abc%20123%20def" so can use it as query parameter in my http request like this

https://myBaseUrl.com?parameter=abc%20123%20def"

how to do that ?

I have tried using this

val myString = "321 pop"
val enCodedString  = URLEncoder.encode(myString, "utf-8")

but if I print, it will produce string like this "321+pop"

Java or Kotlin are OK

Alexa289
  • 8,089
  • 10
  • 74
  • 178

1 Answers1

0

That's expected behavior for encoding the space character. If you want to replace the + with the percent encoding, do

System.out.println(java.net.URLEncoder.encode("Hello World", "UTF-8").replace("+", "%20"));
leeal
  • 16
  • 2