Skip to content

Instantly share code, notes, and snippets.

View ajaypro's full-sized avatar
:octocat:
open source = great knowledge + fast upgrade

AjayDeepak ajaypro

:octocat:
open source = great knowledge + fast upgrade
  • Plume - Smart Wifi Services
  • Amersterdam, Netherlands
View GitHub Profile
@ajaypro
ajaypro / Destructor & partition
Created August 20, 2020 15:12
Destructor declarations and using partition()
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
*/
@ajaypro
ajaypro / kotlin string to date
Created June 13, 2020 17:25
string to date
//string to date
fun main() {
var str = "2020-05-05"
val date = LocalDate.parse(str, DateTimeFormatter.ISO_DATE)
println(date)
}
// 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.)
@ajaypro
ajaypro / kotlin regex usage
Created June 13, 2020 16:06
Using Regex check whether string is numeric
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")
}
@ajaypro
ajaypro / kotlin substringBeforeLast
Created June 13, 2020 12:36
Sample kotlin code of substringBeforeLast()
private const val MY_EMAIL = "ajay@deepak07@gmail.com"
fun main() {
val username = MY_EMAIL.substringBeforeLast(delimiter = '@', missingDelimiterValue = "Username not found")
println("Username -> $username")
}
// Output
Username -> ajay@deepak07
@ajaypro
ajaypro / Kotlin substringAfterLast
Created June 13, 2020 12:31
Sample of kotlin code substringAfterLast()
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
@ajaypro
ajaypro / kotlin substringBefore
Created June 13, 2020 12:28
sample use of substringBefore method
private const val EMAIL = "ajaydeepak07@gmail.com"
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.
@ajaypro
ajaypro / gist:e844a3110d3848038d1136d159a18e15
Created June 13, 2020 12:22
sample of substringAfter method
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
@ajaypro
ajaypro / gist:5c6c82bd0c431ed381d1b2a46508835d
Created April 10, 2020 23:58
Null Coalescing Operator
Can be used in data binding
android:text='@{item.title ?? ""}'
android:text='@{imageProperty.copyright ?? ""}'