Skip to main content
St Louis

Back to all posts

How to Parse A URL String to Get A Domain Only In Kotlin?

Published on
3 min read
How to Parse A URL String to Get A Domain Only In Kotlin? image

Best URL Parsing Tools to Buy in October 2025

+
ONE MORE?

To parse a URL string and extract the domain only in Kotlin, you can follow these steps:

  1. Import the necessary library:

import java.net.URI

  1. Create a function to extract the domain from the URL:

fun getDomainName(url: String): String { val uri = URI(url) val domain = uri.host return if (domain.startsWith("www.")) { domain.substring(4) } else { domain } }

  1. Call the getDomainName() function with the URL string to obtain the domain:

val urlString = "https://www.example.com/some-page.html" val domain = getDomainName(urlString) println(domain) // Output: example.com

In this example, the getDomainName() function takes the URL string as input and utilizes the URI class from the java.net package to parse the URL. The host property of the URI object contains the domain name.

Note that if the URL starts with "www.", the retrieved domain will exclude the "www." prefix. Otherwise, the complete domain will be returned.

What is the path component of a URL?

The path component of a URL is the part of the URL that comes after the domain name and any optional port number. It specifies the location of a specific resource on the server. For example, in the URL "https://www.example.com/blog/article", the path component is "/blog/article". It is used by the server to determine which file or script to serve in response to the request.

How to split a URL string into parts in Kotlin?

In Kotlin, you can split a URL string into its different parts using the URL class provided by the Java standard library. Here's an example of how you can do this:

import java.net.URL

fun main() { val urlString = "https://www.example.com/path/to/resource?key1=value1&key2=value2"

val url = URL(urlString)

val protocol = url.protocol
val host = url.host
val port = url.port
val path = url.path
val query = url.query

println("Protocol: $protocol")
println("Host: $host")
println("Port: $port")
println("Path: $path")
println("Query: $query")

}

This code splits the URL string into its protocol, host, port, path, and query parts. The output of this code will be:

Protocol: https Host: www.example.com Port: -1 Path: /path/to/resource Query: key1=value1&key2=value2

Keep in mind that the port value will be -1 if it is not explicitly specified in the URL string.

How to parse a URL string in Kotlin?

In Kotlin, you can parse a URL string using the java.net.URL class. Here's an example of how you can do it:

import java.net.URL

fun main() { val urlString = "https://www.example.com/path?param1=value1&param2=value2#fragment" val url = URL(urlString)

// Get different parts of the URL
val protocol = url.protocol // "https"
val host = url.host // "www.example.com"
val path = url.path // "/path"
val query = url.query // "param1=value1&param2=value2"
val fragment = url.ref // "fragment"

// Parse Query parameters
val params = mapOf(query.split("&").map { it.split("=").let { (key, value) -> key to value } })
println(params) // prints "{param1=value1, param2=value2}"

// You can also get the URL without the query and fragment
val baseUrl = url.toURI().resolve("/").toURL()
println(baseUrl.toString()) // prints "https://www.example.com/"

}

In this example, we first create a URL object by passing the URL string to its constructor. Then, we can access different parts of the URL like the protocol, host, path, query, and fragment using the appropriate methods (protocol, host, path, query, and ref).

To parse query parameters, we can split the query string by "&" and "=" characters using split, and then create a map with key-value pairs using the resulting list.

You can also get the base URL without the query and fragment by converting the URL object to a URI using toURI(), resolving it with a path delimiter ("/"), and converting back to a URL.

Remember to handle the MalformedURLException and URISyntaxException that might be thrown during parsing.