Quick revision
The short answer
The lines print 4, 2, true true, ababab and 3. trim() only removes characters with a code of U+0020 or below, so the em space U+2003 at the start survives and we are left with the em space, a space and hi. strip() uses Unicode's idea of whitespace and leaves just hi. isBlank() is true for a whitespace-only string and isEmpty() for a zero-length one, repeat(3) repeats the text three times, and lines() splits on \n, \r and \r\n, giving three lines.
What to remember
In this answer, we saw that trim() only removes characters with codes up to U+0020, so it keeps the em space and returns length 4, while strip() uses Character.isWhitespace and leaves hi with length 2, though even strip() keeps the no-break space. isBlank() is true for whitespace-only text and isEmpty() only for length 0, so line three prints true true, repeat(3) gives ababab, and lines() treats \n, \r and \r\n as terminators and counts three lines.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The lines print 4, 2, true true, ababab and 3. trim() only removes characters with a code of U+0020 or below, so the em space U+2003 at the start survives and we are left with the em space, a space and hi. strip() uses Unicode's idea of whitespace and leaves just hi. isBlank() is true for a whitespace-only string and isEmpty() for a zero-length one, repeat(3) repeats the text three times, and lines() splits on \n, \r and \r\n, giving three lines.
Why this question is asked
Cleaning up input is one of the first things any service does, and the difference between trim() and strip() decides whether a value copied from a web page or a PDF matches the value in our database. These methods also came at different times: trim() is from Java 1.0, while strip(), isBlank(), repeat() and lines() arrived in Java 11. Interviewers use a puzzle like this to check whether we know what whitespace means to each method. In this answer, we will recap what counts as whitespace, then go through the five lines in order and explain each result.
1A Quick Recap: Two Definitions of Whitespace
The JDK has two different ideas of whitespace. The old one, used by trim(), is purely numeric: any character with a code of U+0020 (the ordinary space) or less. That covers space, tab, newline and carriage return, but also invisible control characters such as U+0000. It does not cover any of the many space characters Unicode added later.
The newer one, used by strip(), isBlank() and friends, is Character.isWhitespace(). It is true for Unicode space separators, such as the em space U+2003, and for tab, line feed, carriage return and a few other separators, but deliberately false for the no-break spaces such as U+00A0, because those are meant to glue words together. Let us look at the string in the question with this in mind.
| Index | Character | trim() removes? | strip() removes? |
|---|---|---|---|
| 0 | U+2003 em space | No, code is above U+0020 | Yes, isWhitespace is true |
| 1 | U+0020 space | Only if reached from the edge | Yes, from the start |
| 2-3 | h, i | No | No |
| 4 | U+0020 space | Yes, from the end | Yes, from the end |
| 5 | U+0009 tab | Yes | Yes |
trim() stops at index 0 because the em space is not in its definition, so the space at index 1 is protected too.2Line 1: trim() Leaves the Em Space
trim() works inward from both ends and stops at the first character above U+0020. At the start, the very first character is the em space, so it stops immediately and removes nothing, which also protects the ordinary space behind it. At the end, it removes the tab and the space.
This is a classic source of bugs with copied text. A value pasted from a web page or a word processor often carries a Unicode space at the edge, and after trim() it still does not equal the clean value, although both look identical on screen.
3Line 2: strip() Uses Unicode Whitespace
strip() also works inward from both ends, but it asks Character.isWhitespace() for each character. The em space is whitespace by that definition, so it is removed, and so is the space after it. At the end the space and tab go as before.
stripLeading() and stripTrailing() do the same for one side only. Notice the last line: even strip() keeps the no-break space U+00A0, which is common in HTML as . If our input may contain it, we must replace it explicitly, for example with replace('\u00A0', ' ') before stripping.
4Line 3: isBlank() and isEmpty()
isEmpty() has been around since Java 6 and is true only when the length is 0. isBlank(), from Java 11, is true when the string is empty or contains only whitespace in the Character.isWhitespace sense. A single space has length 1, so it is not empty, but it is blank.
So for checking whether a user actually typed something, isBlank() is usually what we mean. It is also cheaper than the old idiom s.trim().isEmpty(), because it does not create a new string, and it handles Unicode spaces that trim() misses.
5Lines 4 and 5: repeat() and lines()
repeat(n) returns the string concatenated n times, so "ab".repeat(3) is ababab. repeat(0) gives an empty string and a negative count throws IllegalArgumentException. It is handy for padding and separators, such as "-".repeat(40).
lines() splits a string into a Stream of lines, treating \n, \r and \r\n all as line terminators, so text from Windows, old Mac and Unix files is handled the same way. The terminators themselves are not part of the lines, and a terminator at the very end does not create an extra empty line.
Because lines() is lazy, it is also a better choice than split("\\R") for long text: it does not build an array of every line first, and it avoids the regex entirely.
6Follow-up questions
When would trim() still be the right choice?
When the format we are handling defines whitespace as ASCII only, or when we need exactly the old behaviour. Many protocols and file formats, such as HTTP headers or fixed-width records, only consider spaces and tabs, and trim() also removes control characters such as U+0000 that strip() leaves alone. Changing trim() to strip() in existing code is also a behaviour change: a value with a leading em space that used to be rejected or stored as-is would now be cleaned, so in legacy code we switch deliberately and with tests.
How do we remove a no-break space, which strip() keeps?
We replace it first, for example s.replace('\u00A0', ' ').strip(), or we normalise all space separators with a regex such as s.replaceAll("[\\h\\s]+", " ").strip(), where \h matches horizontal whitespace including the no-break space. For text that also contains compatibility characters, java.text.Normalizer with the NFKC form turns U+00A0 into an ordinary space before we strip.
Why does lines() not return a trailing empty line?
Because it treats line terminators as ends of lines, not as separators between them. A file that ends with a newline, which is the normal convention on Unix, has the same lines as one that does not. That matches what BufferedReader.readLine() returns, so reading a file and splitting its contents with lines() agree.
7Conclusion
In this answer, we saw that trim() only removes characters with codes up to U+0020, so it keeps the em space and returns length 4, while strip() uses Character.isWhitespace and leaves hi with length 2, though even strip() keeps the no-break space. isBlank() is true for whitespace-only text and isEmpty() only for length 0, so line three prints true true, repeat(3) gives ababab, and lines() treats \n, \r and \r\n as terminators and counts three lines.
Open with
The output is 4, 2, true true, ababab and 3. The key is that Java has two definitions of whitespace. trim, from Java 1.0, removes only characters with a code of U+0020 or below, so at the start it stops immediately at the em space, U+2003, which also protects the normal space behind it; at the end it removes the space and the tab.
Then, if they want more
That leaves em space, space, h, i, so length 4. strip, from Java 11, uses Character.isWhitespace, which includes Unicode space separators like the em space, so it leaves just hi, length 2. stripLeading and stripTrailing do one side; and note that even strip keeps the no-break space, U+00A0, because that is not considered whitespace. isBlank is true for empty or whitespace-only strings, isEmpty only for length zero, so a single space is blank but not empty, and the line prints true true. repeat 3 concatenates the string three times, ababab. lines splits on backslash n, backslash r and backslash r n, without including the terminators, so a, b and c are three lines.
In practice I use strip and isBlank for user input, and trim only when a format defines whitespace as ASCII or when I need to keep legacy behaviour.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 11, Java 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does each line print, and why?": https://scrivio-interview-library.netlify.app/java/strings/b5d9373677/