Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #25350
| From | Jim Janney <jjanney@shell.xmission.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: how to trim() a String only at the right side? |
| Date | 2013-09-26 09:44 -0600 |
| Organization | bilaterally symmetric |
| Message-ID | <ydn4n97oae1.fsf@shell.xmission.com> (permalink) |
| References | <slrnl45qr1.mol.avl@gamma.logic.tuwien.ac.at> <l1vh7v$bjh$1@dont-email.me> |
markspace <markspace@nospam.nospam> writes: > On 9/25/2013 6:55 AM, Andreas Leitgeb wrote: >> I'd need to trim only *trailing* whitespace off a String (or StringBuilder) >> while preserving leading white space. Furthermore (unlike trim()), I'd only >> strip exact \u0020 chars, no other control chars. >> >> I could code a while-loop to search for last non-\u0020 char position and then >> extract the approriate substring (or delete() the spaces), but if there is a >> more elegant solution that I've missed in the javadocs, then I'd be glad about >> a hint. >> >> PS: no need to code out any solutions involving explicit loops. >> PPS: target is Java SE 7 >> > > > Other than download some Apache string utils, I don't think there is > anything better than just using a loop. Most of the examples here > were fairly in efficient: extra concatenation or recursion are going > to be less efficient, potentially *a lot* less efficient. In older versions of Java where substring takes constant time, the time for the recursive version would be proportional to the number of trailing blanks: the main concern would probably be stack overflow. In more recent implementations, where substring does an actual copy, it could potentially take quadratic time--definitely not a good idea. I agree that if speed is wanted a loop is the best approach. The existence of the Apache util suggests that there isn't a good solution in the JRE library. -- Jim Janney
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to trim() a String only at the right side? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-25 13:55 +0000
Re: how to trim() a String only at the right side? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-25 10:25 -0400
Re: how to trim() a String only at the right side? "John B. Matthews" <nospam@nospam.invalid> - 2013-09-25 12:55 -0400
Re: how to trim() a String only at the right side? Jim Janney <jjanney@shell.xmission.com> - 2013-09-25 13:05 -0600
Re: how to trim() a String only at the right side? Silvio <silvio@internet.com> - 2013-09-25 21:46 +0200
Re: how to trim() a String only at the right side? markspace <markspace@nospam.nospam> - 2013-09-25 13:31 -0700
Re: how to trim() a String only at the right side? markspace <markspace@nospam.nospam> - 2013-09-25 13:41 -0700
Re: how to trim() a String only at the right side? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-26 17:11 +0000
Re: how to trim() a String only at the right side? Silvio <silvio@internet.com> - 2013-09-26 12:14 +0200
Re: how to trim() a String only at the right side? Jim Janney <jjanney@shell.xmission.com> - 2013-09-26 09:44 -0600
Re: how to trim() a String only at the right side? Arne Vajhøj <arne@vajhoej.dk> - 2013-09-26 13:01 -0400
Re: how to trim() a String only at the right side? lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-09-26 12:21 +0100
Re: how to trim() a String only at the right side? lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-09-26 17:40 +0100
Re: how to trim() a String only at the right side? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-09-26 16:56 +0000
csiph-web