Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.home.team/?buycode=12345678 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.home.team 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.

sealed class Link {
      data class Buy(val companyCode: String?) : Link()
 }

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
 }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading