Java Reference
In-Depth Information
Trimming Blanks from the End of a String
Problem
You need to work on a string without regard for extra leading or trailing spaces a user may
have typed.
Solution
Use the String class trim() method.
Discussion
Example 3-10 uses trim() to strip an arbitrary number of leading spaces and/or tabs from
lines of Java source code in order to look for the characters //+ and //- . These strings are
special Java comments I use to mark the parts of the programs in this topic that I want to in-
clude in the printed copy.
Example 3-10. GetMark.java (trimming and comparing strings)
public
public class
GetMark {
/** the default starting mark. */
public
class GetMark
public final
final String START_MARK = "//+" ;
/** the default ending mark. */
public
public final
final String END_MARK = "//-" ;
/** Set this to TRUE for running in "exclude" mode (e.g., for
* building exercises from solutions) and to FALSE for running
* in "extract" mode (e.g., writing a book and omitting the
* imports and "public class" stuff).
*/
public
public final
true ;
/** True if we are currently inside marks. */
protected
final static
static boolean
boolean START = true
protected boolean
boolean printing = START ;
/** True if you want line numbers */
protected
protected final
final boolean
boolean number = false
false ;
/** Get Marked parts of one file, given an open LineNumberReader.
* This is the main operation of this class, and can be used
* inside other programs or from the main() wrapper.
*/
public
public void
void process ( String fileName ,
LineNumberReader is ,
Search WWH ::




Custom Search