Diolor
Diolor

Reputation: 13450

Java: Testing if a String ends with a newline

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?

Upvotes: 4

Views: 15373

Answers (5)

Maciej Dobrowolski
Maciej Dobrowolski

Reputation: 12140

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).

Upvotes: 0

bcsb1001
bcsb1001

Reputation: 2907

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.

Upvotes: 8

Elliott Frisch
Elliott Frisch

Reputation: 201527

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 

Upvotes: 0

Kevin Workman
Kevin Workman

Reputation: 42174

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
    }
}

Upvotes: 0

circlespainter
circlespainter

Reputation: 846

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)

Upvotes: 3

Related Questions