2

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

2
  • use a regex expression Commented Sep 20, 2015 at 22:42
  • Look at this answer Commented Sep 20, 2015 at 22:47

2 Answers 2

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

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

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

1 Comment

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
4

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

String trimmed = original.trim();

1 Comment

trim does not remove '\u200B' (zero width space). which is the problem I was having. Using .replaceAll("\\p{C}", "?") from the more upvoted answer worked for me

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.