Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add whitepaces between each capital letter? [closed]

Tags:

java

regex

I try to write a regular expression in order to:

  1. Add white spaces between each capital letter.
  2. Remove all numbers.

I have the text: ClassNameOne839, and I want to get the text: Class Name One

There is a library function which do it? or any regular expression?

like image 636
Or Smith Avatar asked Dec 19 '13 09:12

Or Smith


1 Answers

You can use a combination of replaceAll() calls like this:

String text = "ClassNameOne839";
String cleanText = text.replaceAll("\\d+", "").replaceAll("(.)([A-Z])", "$1 $2");

This first removes all numbers, and then adds a space before all upper-case letters that are not at the beginning of the String. $1 and $2 are references to the first and second groups of the regex.

like image 163
Keppil Avatar answered Sep 27 '22 00:09

Keppil