Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to parse URL in Kotlin

1 Answer

0 votes
import java.net.URI

fun parseUrl(urlString: String) {
    try {
        val uri = URI(urlString)

        println("Scheme: ${uri.scheme}")
        println("Host: ${uri.host}")
        println("Port: ${uri.port}")
        println("Path: ${uri.path}")
        println("Query: ${uri.query}")
        println("Fragment: ${uri.fragment}")
    } catch (e: Exception) {
        println("Invalid URL: $urlString")
    }
}

fun main() {
    val urlString = "https://collectivesolver.com:8080/path?query=param#fragment"
    
    parseUrl(urlString)
}

  
   
/*
run:

Scheme: https
Host: collectivesolver.com
Port: 8080
Path: /path
Query: query=param
Fragment: fragment
 
*/

 



answered Feb 1, 2025 by avibootz
...