In Kotlin, you can use the URLEncoder
class to encode a URL string so that it can be safely transmitted over the internet. The URLEncoder
class is part of the Java standard library, and it provides a method called encode
that can be used to encode a URL string.
Here's an example of how to use the URLEncoder.encode
method to encode a URL string in Kotlin:
import java.net.URLEncoder
fun main() {
val url = "https://www.example.com?q=hello world"
val encodedUrl = URLEncoder.encode(url, "UTF-8")
println(encodedUrl) // prints "https%3A%2F%2Fwww.example.com%3Fq%3Dhello%20world"
}
In this example, we define a URL string that includes a query parameter with a space character. We then use the URLEncoder.encode
method to encode the URL string using the UTF-8 character encoding. The encoded URL is then printed to the console.
Overall, the URLEncoder
class can be used to encode a URL string in Kotlin, ensuring that it can be safely transmitted over the internet.
Top comments (0)