The trim() Method

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

13

Don't forget to count the space.

The trim() Method

Here is a line of the documentation for the String class.

public String trim();

The trim() method of a String object creates a new String that is the same as the original except that any leading or trailing space is trimmed off (removed).

QUESTION 14:

Examine the following code:

String userData = "     745   ";
String fixed;

fixed = userData.trim();

Has the trim() method been used correctly? How many objects have been created?