Just cut the string into two pieces. The first being from the beginning of the original string until the line break and the second from the line break to the end of the string.
Notice that I used System.lineSeparator()
to define which line breaking character defined by the operating system, as this varies from system to system.
This is possible in two ways:
1. Using the split
method to break the string into an array
Note that using this approach, string is going to be broken everywhere a line break is found, this may be an undesirable effect (or may just be 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 substring()
Using this approach you can define how to behave in the event of multiple line breaks.
Explanation of the code:
The variable index
holds the position of the last line break within the main string .
str.substring(0, index)
returns a new string containing a piece of the original string from the beginning (index 0) to the value of index
.
str.substring(index + lineSeparator.length())
returns a new string from the string original line break size (in some systems to break line 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.