2

I have a deleted post about Java where I tried ask the question, "Does String.strip() always remove everything String.trim() does?". This post was marked as a duplicate of this post. I edited my post to clarify that none of the answers on the old post had the answer I was looking for, and that this comment was what inspired the question. Nonetheless, it was deleted.

What should I have done different or better? I feel that my post shouldn't been deleted as the answer was not contained in the related post. Did I not phrase my question or post correctly? Am I just missing something fairly obvious? Should I have commented on that post instead of making a new one? Since the last modification was 2 years ago, I felt okay about making a new question instead of asking in the old one. Am I just misunderstanding what the thresholds are for making a new post instead of commenting on an old one?

1

1 Answer 1

10

This answer on that post already has the answer to your question:

In short: strip() is "Unicode-aware" evolution of trim(). Meaning trim() removes only characters <= U+0020 (space); strip() removes all Unicode whitespace characters (but not all control characters, such as \0)

The last part is what's important: "but not all control characters, such as \0". \0 is provided as a counterexample: it is not removed by strip(), but it is by trim(). So no, not everything removed by trim() will be removed by strip().

I got all that from just that post, so a duplicate here seems valid. I don't necessarily agree with the deletion however, as it could serve as a useful signpost for searches that do not know that that specific question is answered. Maybe a moderator can undelete the post for you.


A reply to the comment you cited also says the same thing. But since it's just a comment, it wouldn't be a valid duplicate target:

apparently trim's set of characters are not a subset of strip's one, since symbol '\u0000' is not deleted by strip, but deleted by trim :-/ ...


If your question was instead about what the full list of characters that trim() removes that strip() doesn't (rather than "are any characters not removed), then that isn't answered yet. But it's also super simple to generate:

for (int i = 0; i <= 0x20; i++) {
  if (!Character.isWhitespace((char) i)) {
    System.out.print(String.format("0x%02X, ", i));
  }
}

produces the results:

0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B

2
  • This isn’t a defense on the deletion but I suspect this is a common misunderstanding so there might be numerous duplicates already. Given there wasn’t much else to the question it’s not that helpful of a signpost. 8 questions already linked to that highly upvoted question. Commented Oct 12 at 14:36
  • 1
    That makes a ton more sense. I don't know really anything about Unicode so I just didn't really get what some parts meant. I probably should've done more digging on how Unicode works before asking. The deletion seems kind of reasonable now. However, these differences between deleting low index and whitespace Unicode characters could definitely be clarified in Java's documentation in the methods themselves.
    – Detinoy
    Commented Oct 12 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .