Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove whitespaces in string with Scala

Tags:

scala

I want to remove the whitespaces in a string.

Input: "le ngoc ky quang"   Output: "lengockyquang" 

I tried the replace and replaceAll methods but that did't work.

like image 925
madagascar Avatar asked Aug 25 '16 03:08

madagascar


People also ask

How do I remove whitespaces from a string?

Use the String. replace() method to remove all whitespace from a string, e.g. str. replace(/\s/g, '') . The replace() method will remove all whitespace characters by replacing them with an empty string.

How do I delete a trailing space in Scala?

To remove blank spaces from a string, Scala provides a trim() method. The trim() will remove spaces from both sides, i.e. before the characters(leading) and from the end(trailing).

How do you cut a space in Scala?

What is trim() in Scala? The trim() method is used to remove all the leading and trailing spaces that are present in the string. For example, if the string looks like, " Hello World " then there is a lot of empty space before the term “Hello” and after the term “World”. The method trim() is used to remove these spaces.

How do you replace all spaces in a string in Java?

In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.


1 Answers

Try the following:

input.replaceAll("\\s", "") 
like image 86
Nyavro Avatar answered Oct 04 '22 16:10

Nyavro