[java] New rule: trim() -> strip()

Replace usage of non unicode aware trim() with unicode aware strip() for a String.

The old String.trim() method is not unicode aware and only removes chacters <= U+0020. In contrast String.strip(), stripLeading() and stripTrailing() recognize all unicode whitespaces.

Non compliant:
String s = " hello world ".trim();

Compliant:
String s = " hello world ".strip();

Type : Security Hotspot… just kidding. Code Smell!
Tags: Java

1 Like

An exception to this rule would be when the code level is less than 11. No issue then.

Hi Marvin,

thanks for sharing! It would indeed be worth having a rule for that. I created a ticket for this rule idea here.

Best,
Marco

3 Likes