Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 26 Sep 2013 06:21:39 -0500 Date: Thu, 26 Sep 2013 12:21:37 +0100 From: lipska the kat <"nospam at neversurrender dot co dot uk"> Organization: Trollbusters 3 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: how to trim() a String only at the right side? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: Lines: 43 X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-Bnr/jvdsx/qsNVfmto698vsaKel0fiUSfBYjuP4H5GMqyeuGxYPrapesioOkqYaNc1stSLF/moFfuwP!pwZxg2kkfpEpVx/y3a+xrFjPpHD5svvpkw7b90U3AOS2+89IhLfHoMhPIYAw5lgrwqXhalg0FEI= X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2417 Xref: csiph.com comp.lang.java.programmer:25348 On 25/09/13 14:55, 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 > package cljp; import org.apache.commons.lang3.StringUtils; public class Stripper { /** * @param args */ public static void main(String[] args) { String foo = " foo "; String bar = "bar"; System.out.print(foo); System.out.println(bar); foo = StringUtils.stripEnd(foo, " "); System.out.print(foo); System.out.println(bar); } } -- Lipska the KatŠ: Troll hunter, sandbox destroyer and farscape dreamer of Aeryn Sun GNU/Linux user #560883 - http://www.linuxcounter.net