Skip to main content
ubuntuask.com

Back to all posts

How to Remove All the Whitespace From A String In Kotlin?

Published on
3 min read
How to Remove All the Whitespace From A String In Kotlin? image

Best Kotlin Programming Tips to Buy in October 2025

1 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
2 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
3 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
4 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.62
Kotlin: An Illustrated Guide
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Atomic Kotlin

Atomic Kotlin

BUY & SAVE
$44.91 $49.00
Save 8%
Atomic Kotlin
7 Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

BUY & SAVE
$30.53 $44.99
Save 32%
Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices
8 Kotlin in Action

Kotlin in Action

BUY & SAVE
$34.71 $44.99
Save 23%
Kotlin in Action
+
ONE MORE?

To remove all whitespace in a string using Kotlin, you can make use of the replace function with a regular expression pattern. Here's the code snippet that demonstrates how to remove whitespace from a given string:

val originalString = "Hello World" val stringWithoutSpaces = originalString.replace("\\s".toRegex(), "")

In this code, we declare a variable originalString which holds the original string containing whitespace. The replace function is then used on this string, where the first parameter "\\s".toRegex() represents a regular expression pattern matching any whitespace character. The second parameter is an empty string, indicating the replacement.

After executing the code, the variable stringWithoutSpaces will contain the modified string with all the whitespace removed. In this case, the output will be "HelloWorld" as all the spaces between the words have been eliminated.

By using this approach, you can easily remove all types of whitespace characters from a string in Kotlin.

How can I remove space characters, including non-breaking spaces, from a string in Kotlin?

In Kotlin, you can remove space characters from a string, including non-breaking spaces, by using the replace function with a regular expression.

Here's an example of how you can achieve this:

val text = "Hello World\u00A0!" // Including regular space and non-breaking space val result = text.replace("\\s".toRegex(), "") println(result) // Output: HelloWorld!

In the above code, text is the input string containing regular spaces and a non-breaking space (\u00A0). The replace function is called on the text string with a regular expression "\\s".toRegex(). The regular expression \\s matches any whitespace character, including regular space, tab, and newlines.

The replace function replaces all matches of the regular expression with an empty string, effectively removing them from the original string.

The resulting string is stored in the result variable, and when printed, it will output HelloWorld! with all the spaces removed, including the non-breaking space.

How can I remove all whitespaces, including non-breaking spaces, from a string but preserve line breaks in Kotlin?

One way to remove all whitespaces, including non-breaking spaces, from a string while preserving line breaks in Kotlin is by using regular expressions. Here's an example:

fun removeSpacesPreserveLineBreaks(input: String): String { // Replace all whitespaces including non-breaking spaces with an empty string val regex = Regex("[\\s\\u00A0]+") val result = input.replace(regex, "")

return result

}

Here, the regular expression [\\s\\u00A0]+ matches any whitespace character (\\s) and non-breaking space (\\u00A0) one or more times. The replace function is then used to replace all the matches with an empty string.

Example usage:

val input = """Hello, |World!""" val output = removeSpacesPreserveLineBreaks(input)

println(output)

This will output:

Hello, World!

Line breaks will be preserved, but all whitespaces, including non-breaking spaces, will be removed.

What is the Kotlin function to delete leading and trailing spaces from a string?

The Kotlin function to delete leading and trailing spaces from a string is trim().