From the course: Java Cheat Codes: Most Popular Functions
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: trim() - Java Tutorial
From the course: Java Cheat Codes: Most Popular Functions
Solution: trim()
(upbeat music) - [Instructor] Okay. Here is one way you can solve the trim method challenge. We start off with our answer class, our given string and constructor. And scrolling down, here is what we would do to remove leading and trailing whitespace from the original string, lines 12 through 15. We have the public String trimWhitespace, and what we would do to return is return originalString.trim. And here's where we're able to utilize the trimMethod. As we saw in our answer class provided, we were able to remove specified characters from the original string and then access the original string. And then I think the meat and potatoes is really in the tester class. Here is where we have our "Hello, world!" available to us, with spaces already in there. And then to trim the original string, what you would do is you'd do string, trimmedString = answer.trimWhitespace. And then you want to output what you have, just to test it. So "Trimmed string: " plus the trimmedString method that we…