How to get the first portion of a string in a uri authority (android)

Issue

I am currently working on a task and wondering how I can achieve this implementation. I have this unique URL, fake url here https://home.ab.com what I am trying to print out, is the first home after // not the home after the .ab let’s call it company code. I know in a Uri, I can get the authority which as per documentation it returns this entire part home.ab.com My use case though is I only want to print out home based on unique companies and situation we support. How can I achieve this. This is what I have so far.

my function getCode(uri) now where I am trying to get home is where I am stuck how can I get and return the home without the other part?

private fun getCode(uri: Uri): String{
   // todo
 }

Solution

Try this code:

private fun getCode(uri: Uri): String{
   val s  uri.toString()
   val start  s.indexOf('/') + 2
   val end  s.indexOf('.')
   return s.substring(start, end)
}

Playground

Answered By – Arpit Shukla

Leave a Comment