3

I struggle to find a Guava equivalent for commons-lang StringUtils.removeEnd. Is there such a method or do I have to use a Joiner and a Splitter in some way?

Tim Büthe
  • 62,884
  • 17
  • 92
  • 129

1 Answers1

2

I don't think Guava provides such a method, but it's a trivial one-liner:

s = s.endsWith(suffix) ? s.substring(0, s.length() - suffix.length()) : s;
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 2
    StringUtils handles null inputs as well, so it might be a little more complicated. Besides, using the util method "removeEnd" increases readability. – Tim Büthe May 23 '14 at 09:54
  • 1
    Guava, in general, doesn't try to be null-safe. Your code should take care of that. Nothing forbids you to put that one-liner into a method. Guava doesn't provide it, probably because it's an uncommon use-case that is trivial to implement with standard String methods. – JB Nizet May 23 '14 at 10:00