3

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?

1
  • Can you post an MCVE with hardcoded Strings? We have no idea what nextResult is, so this is ipmossible to test. Sep 9, 2014 at 15:52

5 Answers 5

7

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.

3

Doesn't this work? Seems to be there at least since 1.5.0 http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#endsWith(java.lang.String)

1
  • 2
    .endsWith("\n") is perfect. +1
    – Diolor
    Sep 9, 2014 at 16:09
0

I would guess that something is wrong with your nextResult variable, since this works fine for me:

public class Test{
    public static void main(String... args){
        System.out.println("\t".equals("\n")); //false
        System.out.println(" ".equals("\n")); //false
        System.out.println("\n".equals("\n")); //true
    }
}

Make sure that nextResult really contains what you think it does, and if so, post an MCVE that uses hardcoded Strings to show us exactly what's going wrong.

Edit: I've modified the above example to use a substring, and it still works fine:

public class Test{
    public static void main(String... args){

        String endsWithNewline = "test\n";
        String substring = endsWithNewline.substring(4);

        System.out.println(substring.equals("\t")); //false
        System.out.println(substring.equals(" ")); //false
        System.out.println(substring.equals("\n")); //true
    }
}
1
  • 1
    Yes you are correct. Equals works perfectly. I had a tree iteration which was concat-ing strings in abnormal way so there was always a \n at the end.
    – Diolor
    Sep 9, 2014 at 16:08
0

Your question doesn't make much sense, and your input(s) cannot be what you seem to think they are -

// The first one will equal "\n" the second won't.
String[] arr = { "hi\n", "hi " };
for (String nextResult : arr) {
    String substring = nextResult.substring(nextResult.length() - 1);
    if (substring.equals("\n")) {
        System.out.println("Yes: " + nextResult);
    } else {
        System.out.println("No: " + nextResult);
    }
}

Output is

Yes: hi

No: hi 
0

For the purpose of checking if something is a new line symbol I would use Guava CharMatcher class.

final String breakingWhitespace = "\n\r";
Assert.assertTrue(CharMatcher.BREAKING_WHITESPACE.matchesAllOf(whitespace));

There are also many other variations of matching:

  • matches(char)
  • matchesAllOf(CharSequence)
  • matchesAnyOf(CharSequence)
  • matchesNoneOf(CharSequence)

Look here for documentation.

The main advantage of using this approach would be that it fits really many "new line" (or whitespace) characters (just look at the code).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.