Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin string extension to apply https prefix to string.
/**
* Adds https prefix if link does not have prefix. It will also change old prefix http to https.
* @return link/url with https prefix
*/
fun String.toHttpsPrefix(): String? = if (isNotEmpty() && !startsWith("https://") && !startsWith("http://")) {
"https://$this"
} else if (startsWith("http://")) {
replace("http://", "https://")
} else this
@martinloesethjensen
Copy link
Author

martinloesethjensen commented Apr 5, 2020

You can also specify this on the function calls:

this.isNotEmpty()

@martinloesethjensen
Copy link
Author

martinloesethjensen commented Apr 5, 2020

You can test it out in the Kotlin Playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment