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
Moving to last element in array and moving towards left till we reach an even element | |
int[] arr = {1, 2, 3, 4, 5}; | |
int i = arr.length -1; | |
while(i>=0 && arr[i] % 2 == 1) | |
i--; | |
------------------------------------------------ |
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
Can be used in data binding | |
android:text='@{item.title ?? ""}' | |
android:text='@{imageProperty.copyright ?? ""}' |
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 FILE_PATH = "/storage/emulated/0/DCIM/Gallery/erwfwet285d9cesdferqwrsdfsg7e3259f.mp4" | |
fun main() { | |
val extension = FILE_PATH.substringAfter(delimiter = ".", missingDelimiterValue = "Extension Not found") | |
println("File extension -> $extension") | |
} | |
// Output | |
File extension -> mp4 | |
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 EMAIL = "[email protected]" | |
fun main() { | |
val username = MY_EMAIL.substringBefore(delimiter = "@", missingDelimiterValue = "Usernmae Not Found") | |
println("Username -> $username") | |
} | |
// Output | |
Username -> ajaydeepak07 | |
Explanation: the above program returns the substring before the appearance of @ character. |
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 FILE_PATH = "/storage/emulated/0/DCIM/Gallery/.2394823usfdfh234uu9sd2333.mp4" | |
fun main() { | |
val fileExtension = FILE_PATH.substringAfterLast(delimiter = ".", missingDelimiterValue = "Extension Not found") | |
println("File extension -> $fileExtension") | |
} | |
// Output | |
File extension -> mp4 | |
Explanation: the substringAfterLast method successfully gives the file extension | |
although the FILE_PATH contains two delimiter value well, that’s because the method |
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 | |
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
fun main() { | |
val str = "12345" | |
var numeric = false | |
numeric = str.matches("-?\\d+(\\.\\d+)?".toRegex()) | |
if (numeric) | |
println("$str is a number") | |
else | |
println("$str is not a number") | |
} |
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
// replace whitespaces in string | |
fun main() { | |
var str = "T his is b ett er." | |
var pharse = str.replace("\\s".toRegex(),"") | |
println(pharse) | |
} | |
We've used regular expression \\s that finds all white space characters (tabs, spaces, new line character, etc.) |
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
//string to date | |
fun main() { | |
var str = "2020-05-05" | |
val date = LocalDate.parse(str, DateTimeFormatter.ISO_DATE) | |
println(date) | |
} |
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
data class Bus(val model:String, val route:Int, val isSpecialService: Boolean) | |
val volvo = Bus("volvo", 23, true) | |
val leyland = Bus("leyland", 45, false) | |
val benz = Bus("benz", 109, true) | |
val scania = Bus("scania", 901, false) | |
/** | |
* Usage of partition and also destructing constructors | |
*/ |