-2

I'm trying to make the first letter of a sentence uppercase and the rest lowercase using regex in Java.

I know functions like toLowerCase and toUpperCase exist but I MUST use regex for academic reasons.

If a word except the first word starts with a capital letter or written in mixed case, the word should be converted to all lowercase letters. The first word should start with a capital letter

john WaLKs into A New world. ----> John walks into a new world.

I've tried using something like this but it doesn't work and I don't know what to do.

data.replaceAll("([A-Z])","$1\L");

  • Do you mean `data = data.replaceAll("([A-Z])","$1\L");`? Strings are immutable, so `replaceAll` doesn't actually update the string directly. – Andy Turner Oct 27 '19 at 15:31
  • I have to use regex otherwise I get a 0. I know functions like toLowercase exist. Also I use data = data.replaceAll and the regex is wrong. – Selcuk Coskun Oct 27 '19 at 15:33
  • Please quote your exercise (especially part about using regex) because what you want to do doesn't seem to make much sense. – Pshemo Oct 27 '19 at 15:42

1 Answers1

0

You should make it by passes, I mean first convert all to lowercase then Capitalize it