The Java String trim function is a built-in method that removes any leading and trailing white spaces from a given string. It returns a new string with the white spaces removed. The trim function is useful when dealing with user input or when parsing data from external sources, as it ensures that any extra spaces are removed before processing the data. The trim function can be called on any string object and does not modify the original string. Keep reading below to learn how to Java String trim in Kotlin.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Java String trim in Kotlin With Example Code

Java String trim() method is used to remove the leading and trailing white spaces from a string. In Kotlin, we can use the same method to trim a string. In this blog post, we will discuss how to Java String trim in Kotlin.

To trim a string in Kotlin, we can use the trim() method of the String class. This method returns a new string with leading and trailing white spaces removed. Here is an example:


val str = " Hello, World! "
val trimmedStr = str.trim()
println(trimmedStr) // Output: "Hello, World!"

In the above example, we have a string with leading and trailing white spaces. We use the trim() method to remove the white spaces and assign the result to a new variable. Finally, we print the trimmed string.

We can also use the trimStart() and trimEnd() methods to remove only the leading or trailing white spaces, respectively. Here is an example:


val str = " Hello, World! "
val trimmedStartStr = str.trimStart()
val trimmedEndStr = str.trimEnd()
println(trimmedStartStr) // Output: "Hello, World! "
println(trimmedEndStr) // Output: " Hello, World!"

In the above example, we use the trimStart() method to remove the leading white spaces and assign the result to a new variable. Similarly, we use the trimEnd() method to remove the trailing white spaces and assign the result to another variable. Finally, we print both trimmed strings.

In conclusion, Java String trim() method can be used in Kotlin to remove the leading and trailing white spaces from a string. We can also use the trimStart() and trimEnd() methods to remove only the leading or trailing white spaces, respectively.

Equivalent of Java String trim in Kotlin

In conclusion, the Kotlin programming language provides a convenient and efficient way to trim strings using the built-in trim() function. This function removes any leading and trailing whitespace characters from a string, making it easier to work with and manipulate. The trim() function in Kotlin is equivalent to the trim() function in Java, but with the added benefit of being more concise and readable. Whether you are a beginner or an experienced developer, using the trim() function in Kotlin can help you write cleaner and more efficient code. So, if you’re looking for a simple and effective way to trim strings in Kotlin, the trim() function is definitely worth considering.

Contact Us