Write a method, getSecondLine, that is passed a String argument and that returns the second line, without its newline character.

Write a method, getSecondLine, that is passed a String argument and that returns the second line, without its newline character.

LANGUAGE: Java

CHALLENGE:

Write a method, getSecondLine, that is passed a String argument and that returns the second line, without its newline character.
(Recall that lines are terminated with the “\n” character .) Assume that the argument contains at least two complete, newline-terminated lines.

SOLUTION:


public String getSecondLine(String aString) {
    return aString.substring(aString.indexOf("\n") + 1,aString.indexOf("\n") + 1 + aString.substring(aString.indexOf("\n") + 1, aString.length()).indexOf("\n"));
}

Add comment