在Android Kotlin中,可以使用URLEncoder来对字符串进行URL编码。

以下是一个示例代码:

import java.net.URLEncoder

fun main() {
    val str = "Hello World!"
    val encodedStr = URLEncoder.encode(str, "UTF-8")
    println("Encoded String: $encodedStr")
}

输出结果:

Encoded String: Hello%20World%21

在上面的代码中,我们使用URLEncoder.encode方法来对字符串进行URL编码。第一个参数是要编码的字符串,第二个参数是编码格式,常用的是UTF-8

在输出结果中,空格被编码为%20,感叹号被编码为%21

Android Kotlin 字符串urlcode编码

原文地址: http://www.cveoy.top/t/topic/iZJb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录