Skip to content

Instantly share code, notes, and snippets.

@ajaypro
Created June 13, 2020 12:36
Show Gist options
  • Save ajaypro/34b80793fd7c46fbabadc3d75a31059e to your computer and use it in GitHub Desktop.
Save ajaypro/34b80793fd7c46fbabadc3d75a31059e to your computer and use it in GitHub Desktop.
Sample kotlin code of substringBeforeLast()
private const val MY_EMAIL = "ajay@[email protected]"
fun main() {
val username = MY_EMAIL.substringBeforeLast(delimiter = '@', missingDelimiterValue = "Username not found")
println("Username -> $username")
}
// Output
Username -> ajay@deepak07
Explanation: that’s not a valid email address but this is only for example.
The moment substringBeforeLast method finds the first @ delimiter from the right
side even though the MY_EMAIL has two @ delimiters. It’ll return the remaining left
side substring without going further.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment