1

how can i find the last visible character in a Java String? I'd like to remove all line breaks and other invisible characters from a Java String

Kind Regards, Tord

sunyata
  • 1,563
  • 2
  • 23
  • 36

2 Answers2

9
string_variable.replaceAll("\\p{C}", "?");

This will replace all non-printable characters. Where p{C} selects the invisible control characters and unused code points.

  • Thank you for your help, unfortunately this will remove all characters, i'm trying to find the last visible character in an article (like maybe a period or a letter or a number). Then i can remove the rest of the characters that are coming after that – sunyata Sep 20 '15 at 22:49
3

You can use the trim() method of the String class for removing trailing (and leading) white space and line breaks:

String trimmed = original.trim();
Mick Mnemonic
  • 7,638
  • 2
  • 24
  • 30