Just cut the string into two pieces. The first being from the beginning of the original string to the line break and the second from the line break to the end of the string.
Notice that I used System.lineSeparator()
to define the line break character defined by the operating system, as this varies from one system to another.
This is possible in two ways:
1. Using the method split
to break the string into a array
Note that using this approach, the string will be broken everywhere a line break is found, this may be an undesirable effect (or it may be just what you are looking for).
public class Main {
public static void main(String[] args){
String lineSeparator = System.lineSeparator();
String str = "REMETIDOS OS AUTOS PARA DISTRIBUIDOR" + lineSeparator +
"Baixa novamente";
String[] arr = str.split(lineSeparator);
String titulo = arr[0];
String descricao = arr[1];
System.out.println(titulo);
System.out.println(descricao);
}
}
See working on repl.it.
2. Using the method substring()
Using this approach it is possible to define how to behave in the case of multiple line breaks.
Explanation of the code:
The variable index
guard the position of the last line break within the string leading.
str.substring(0, index)
returns a new string containing a piece of string original, from the beginning (index 0) to the value of index
.
str.substring(index + lineSeparator.length())
returns a new string from the line break of the string original plus line break size (in some systems the line break can be represented by two characters), this to ignore the line break in the new strings.
public class Main {
public static void main(String[] args){
String lineSeparator = System.lineSeparator();
String str = "REMETIDOS OS AUTOS PARA DISTRIBUIDOR" + lineSeparator +
"Baixa novamente";
int index = str.lastIndexOf(lineSeparator);
String titulo = str.substring(0, index);
String descricao = str.substring(index + lineSeparator.length());
System.out.println(titulo);
System.out.println(descricao);
}
}
See working on repl.it.