MainActivity.kt
package com.cfsuman.kotlintutorials
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
val textView = findViewById<TextView>(R.id.textView)
// make textview content scrollable
textView.movementMethod = ScrollingMovementMethod()
// drop example
val string = "This is a sample string."
// Returns a string with the first n characters removed.
val result = string.drop(5)
textView.text = string
textView.append("\nAfter dropping 5 chars")
textView.append("\n$result")
// drop last example
val string2 = "This is another sample string."
// Returns a string with the last n characters removed.
val result2 = string2.dropLast(6)
textView.append("\n\n" + string2)
textView.append("\nAfter dropping last 6 chars")
textView.append("\n$result2")
// drop while example
val string3 = "This is a example string."
val result3 = string3.dropWhile{
it == 'T'
}
textView.append("\n\n" + string3)
textView.append("\nDrop while first char is 'T'")
textView.append("\n$result3")
// drop last while example
val string4 = "This is a example text."
val result4 = string4.dropLastWhile{
it == '.'
}
textView.append("\n\n" + string4)
textView.append("\nDrop last while last char is '.'")
textView.append("\n$result4")
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F8F8F8"
android:padding="24dp">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="sans-serif"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

- kotlin ktx - Drawable to bitmap
- kotlin syntax - String all and any
- kotlin syntax - String chunked
- kotlin syntax - String capitalize decapitalize
- kotlin syntax - Get string last index
- kotlin syntax - Compare two strings
- kotlin syntax - Filter string
- kotlin syntax - String isBlank and isEmpty
- kotlin syntax - Split string into lines
- kotlin syntax - String padStart and padEnd