In general both method removes leading and trailing spaces from string. However the difference comes when we work with unicode charaters or multilingual features.
trim() removes all leading and trailing character whose ASCII value is less than or equal to 32 (‘U+0020’ or space).
According to Unicode standards there are various space characters having ASCII value more than 32(‘U+0020’). Ex: 8193(U+2001).
use
String.strip
instead ofString.trim
to remove all Unicode spaces:https://jkorpela.fi/chars/spaces.html
https://stackoverflow.com/questions/51266582/difference-between-string-trim-and-strip-methods-in-java-11