String.ltrim() and rtrim() methods RFE

Rémi Forax forax at univ-mlv.fr
Sat Nov 17 14:08:19 UTC 2007


Mark Reinhold a écrit :
>> Date: Fri, 16 Nov 2007 15:19:50 -0800
>> From: Nick Radov <nradov at axolotl.com>
>>     
>
>   
>> The bug voting mechanism doesn't really work for trivial RFEs like this.
>> First, the bug has been closed for a while and no one is going to vote for
>> a closed bug. Second, everyone only gets a few votes so they're going to
>> put their votes on the most critical issues. Annoyances like this are left
>> to languish.
>>     
>
> Agreed.
>
>   
>> Let's look at this RFE a different way. Is there any reason not to
>> implement it?
>>     
>
> Beware: In general this is not a very persuasive line of reasoning.
>
> If all RFEs over the last ten years had been evaluated in this way then
> most of them would've been implemented by now, and the platform would be
> a horrid, rotting mess of woefully inconsistent spaghetti.
>
> Having said that, I've spent quite a bit of time over the last couple of
> months hacking Python code for the OpenJDK Mercurial infrastructure, and
> I've used Python's equivalent lstrip/rstrip functions more than once.
> They're quite handy actually, especially the rstrip function that takes
> a string argument and removes any trailing characters present in that
> string.
>
> So if somebody's going do to this I'd actually recommend adding four
> methods (needed since Java doesn't have default parameter values):
>
>     String.ltrim()
>     String.ltrim(String charsToTrim)
>     String.rtrim()
>     String.rtrim(String charsToTrim)
>   
we don't have default values but we have varargs :)
so we can mimic python lstrip/rstrip using only two methods:

     String.ltrim(char... charsToTrim)
     String.rtrim(char... charsToTrim)

If the array charsToTrim is empty or null, whitespace characters are used.
> Of course one can get this behavior today with the replaceFirst method,
> e.g.,
>
>     s.replaceFirst("[charsToTrim]+$", "")
>
> but that requires compiling the regex and building a matcher, which is
> awfully heavyweight, especially in the middle of a tight loop.
>
> - Mark
>   
Rémi



More information about the core-libs-dev mailing list