47

I have a string like

String  str = "My name is Monda"

How can I achieve a string like

str  = "MynameisMonda"

2 Answers 2

67

You can use the replaceAll() function.

For your case:

replaceAll("\\s","")

where \s means any whitespace (such as a space character).

Sign up to request clarification or add additional context in comments.

1 Comment

If you use this line breaks will be removed as well. In my case this behavior triggered errors further down the groovy script
41

You just need this function. replaceAll()

str.replaceAll("\\s","")

\s = Anything that is a space character (including space, tab characters etc)

You need to escape the backslash if you want \s to reach the regex engine, resulting in \s. Like wise we use :-

\S = Anything that isn't a space character (including both letters and numbers, as well as punctuation etc)

\w = Anything that is a word character

\W = Anything that isn't a word character (including punctuation etc)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.