kotlin string remove last 3 digits

To remove the last three digits from a Kotlin string, you can use the substring function. Here are the steps to achieve this:

  1. Declare a string variable: Start by declaring a string variable and assigning it the desired value. For example, val str = "123456789".

  2. Get the substring: Use the substring function to extract a substring from the original string. The substring function takes two parameters: the starting index and the ending index (exclusive). In this case, you want to remove the last three digits, so you need to get the substring from the beginning of the original string to the length of the original string minus three. You can achieve this by using str.substring(0, str.length - 3).

  3. Print or store the result: You can either print the resulting substring using println or store it in another variable for further use. For example, val result = str.substring(0, str.length - 3).

Here is the complete code snippet:

val str = "123456789"
val result = str.substring(0, str.length - 3)
println(result)

This code will output 123456 as the result, which is the original string without the last three digits.