Created
June 13, 2020 12:36
-
-
Save ajaypro/34b80793fd7c46fbabadc3d75a31059e to your computer and use it in GitHub Desktop.
Sample kotlin code of substringBeforeLast()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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