Skip to main content

Posts

Showing posts with the label Kotlin String

android kotlin - How to get substring from string

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() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry." textView.text = string // Returns a substring of this string that starts at the // specified startIndex and continues to the end of the string. val result = string.substring( 50 // ...

android kotlin - String replace range example

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() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry. Lorem Ipsum" + " has been the industry's standard dummy text" + " ever since the 1500s." textView.text = string // The end index of the range is included ...

android kotlin - String replace example

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() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry. Lorem Ipsum" + " has been the industry's standard dummy text" + " ever since the 1500s." textView.text = string /* documentation source kotlin...

android kotlin - String remove surrounding example

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() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry. Lorem Ipsum" textView.text = string /* documentation source kotlinlang.org Removes the given delimiter string from both the start and the end of this string if and o...

android kotlin - String remove range example

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() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry." textView.text = string // Removes the part of a string at the given range. // The end index of the range is included in the removed part. val result = string.removeRange( 10..50 //...

android kotlin - String remove prefix and suffix example

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() val string1 = "This is a sample string to" + " test remove prefix function." textView.text = string1 /* documentation source kotlinlang.org If this string starts with the given prefix, returns a copy of this string with the prefix removed. Otherwise, ret...

android kotlin - How to match two strings region

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() val string1 = "This is a sample string to test the function." val string2 = "This is another SAMPLE string to test the " + "string region matches method." textView.text = string1 textView.append("\n\n") textView.append(string2) /* documentation so...

android kotlin - Prepend indent to string every line

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() val string = "Lorem Ipsum is simply dummy text of the" + " printing and \ntypesetting industry. Lorem Ipsum" + " has been the industry's \nstandard dummy text" + " ever since the 1500s, when \nan unknown printer" + " took a galley of type and \...

android kotlin - How to partition a string

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() val string = "This is A SAMple striNg." textView.text = string /* documentation source: kotlinlang.org Splits the original char sequence into pair of char sequences, where first char sequence contains characters for which predicate yielded true, while second char sequen...

android kotlin - String padStart and padEnd example

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() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry." textView.text = string // string is returned as is, when its // length is greater than the specified val afterPadStart10 = string.padStart(10,'*') textView.append("\n\nAfte...

android kotlin - How to split string into lines

MainActivity.kt package com.example.jetpack import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val string = "Lorem Ipsum is simply dummy text \nof the" + " printing and typesetting industry. Lorem" + " Ipsum has been the industry's \nstandard dummy" + " text ever since \nthe 1500s, when an unknown" + " printer took a galley of type \nand scrambled" + " it to make a type specimen book." textView.text = string /* source: kotlinlang.org Splits this char sequence to a list of lines delimited by any of the following...

android kotlin - String isBlank and isEmpty example

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() val string = " " textView.text = "This string ($string)" + " length is " + string.length // Returns true if this string is empty // or consists solely of whitespace characters. textView.append("\n\nString is blank? ") textView.append(string.isBlank().toStr...

android kotlin - How to filter string

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() val string = "Lorem Ipsum is simply dummy text of the printing" + " and typesetting industry. Lorem Ipsum has been the" + " industry's standard dummy text ever since the 1500s," + " when an unknown printer took a galley of type" + " and scramble...

android kotlin - How to compare two strings

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() val string1 = "This is a sample string." val string2 = "This is a SAMPLE string." textView.text = "String 1 : \n$string1" textView.append("\n\nString 2 : \n$string2") // Compares two strings lexicographically, // optionally ignoring case differences. if (string1.com...

android kotlin - Get string last index

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() val string = "Lorem Ipsum is simply dummy text of the printing" + " and typesetting industry. Lorem Ipsum has been the" + " industry's standard dummy text ever since the" + " 1500s, when an unknown printer took a galley" + " of type and scramble...

android kotlin - String capitalize decapitalize example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.util.* 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() val string = "this is a sample string." textView.text = string textView.append("\n\nAfter capitalize it....") // Returns a copy of this string // having its first letter uppercase. textView.append("\n" + string.replaceFirstChar { if (it.isLowerCase()) it.t...

android kotlin - String chunked function example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.util.* 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() val string = "Lorem Ipsum is simply dummy text of the" + " printing and typesetting industry." textView.text = string // Splits this char sequence into a list // of strings each not exceeding the given size. val list:List<String> = string.chunked(15) //...

android kotlin - String all and any example

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() val string = "This is Android SDK 33" textView.text = string // Returns true if all characters match the given predicate. val result:Boolean = string.all{ it.isDigit() } textView.append("\n\nIs this string contain only digit? $result") // Returns true if at least one chara...

android kotlin - String Drop DropLast DropWhile DropLastWhile example

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 ...

Android kotlin: How to iterate over the characters in a string

Iterating Over Strings in Android with Kotlin This code demonstrates two ways to iterate over the characters in a string within an Android application written in Kotlin. The application presents two buttons: "Iterate over string": Clicking this button iterates through the characters of a string variable without using an index property. "Iterate string using index": Clicking this button iterates through the characters using the string's index property. Both button clicks update a TextView element, displaying the characters of the string one by one on separate lines. Breakdown and Functionality The code consists of two main parts: the MainActivity.kt file containing the Kotlin code and the activity_main.xml file defining the user interface layout. MainActivity.kt: Imports: The code imports necessary libraries for building the user interface and working with resources. Class Definition: The MainActivity class inherits from AppCompatActivity , which i...