trimming : Java Glossary
home T words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
trimming
To remove unwanted waste from the beginning or end of something. In addition to the built-in Java trimming methods, I have written a some myself and posted them as part of the com.mindprod.common11.StringTools class that you can download including source.
Java Trimming Methods
Method Function
ArrayList.trimToSize chops off the unused RAM on ArrayList’s internal array by allocating a new array just the right size and copying the elements over, then discarding the old internal array to be garbage collected.
Collection.toArray Exports an array just the right size. Always provide it with a template array the right size and type e.g.
String.trim removes both leading and trailing whitespace (not just blanks, nul, \n, \t…all the chars in the range 0..0x20) and gives you a new substring of the orginal. trim is neatly implmented. If there is nothing to trim, it just gives you your original String back. If there is something to trim, it gives you a substring view of your original immutable String. It does not need to allocate a new internal char[] or copy over the meat characters. In other words trim is a fast operation.
StringTools.condense collapses multiple spaces to a single space and trims off both leading and trailing spaces (not whitespace).
StringTools.countLeading count how many of a leading character there are, e.g. how many spaces or '*'s.
StringTools.countTrailing counts how many of a trailing character there are, e.g. how many spaces or '*'s.
StringTools.squish removes all spaces (not whitespace), included embedded ones.
StringTools.trimLeading removes just leading whitespace.
StringTools.trimTrailing removes just trailing whitespace.

Learning More

Sun’s Javadoc on ArrayList.trimHtoSize : available:
Sun’s Javadoc on String.trim : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] The information on this page is for non-military use only.
You are visitor number 5,742. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/trimming.html J:\mindprod\jgloss\trimming.html