I want to perform a substring.equals("\n")
. In the code below, I take the last character and check if it is a newline.
String substring = nextResult.length() > 1 ? nextResult.substring(nextResult.length() - 1) : nextResult;
return substring.equals("\n") ? /* do stuff */ : /* do other stuff */;
I take only the last character because Java takes \n
as one char
. However, from what I see, substring.equals("\n")
returns true
for whitespaces (" "
), and I think tabs (\t
). Is that so?
How can I correctly check if the end of a string is a newline, or at least if the string is a newline?
You may use String#endsWith
:
boolean endsWithNewline = nextResult.endsWith("\n");
Or String#charAt
:
boolean endsWithNewLine = nextResult.charAt(nextResult.length() - 1) == '\n';
However, your current code works fine for me. Perhaps there is some kind of typo in your inputs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With