David Studer
David Studer

Reputation: 135

Set period between lowercase letter followed by uppercase letter in Java

Is there a way to use str.replaceAll() function with regular expression to set a period and space (". ") between each lowercase letter followed by an uppercase letter? Or is there another way to achieve the following transformation?

e.g. "First sentenceSecond sentenceLast sentence" to "First sentence. Second sentence. Last sentence"

Upvotes: 2

Views: 301

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59988

You can use replaceAll with this regex ([a-z])([A-Z]) which will match two groups the first one is the lowercase letter the second the uppercase letter, then replace them with $1. $2 like so :

String str = "First sentenceSecond sentenceLast sentence";

str = str.replaceAll("([a-z])([A-Z])", "$1. $2");

Output

First sentence. Second sentence. Last sentence

Upvotes: 6

Related Questions