Separate sentences into a String

1

I have this String with a space between the title (upper case) and the description:

  

RETURNED CARS FOR DISTRIBUTOR
  Download again

The code:

final String linha = row.getCell(3).getTextContent().replaceAll("\t","").replaceAll("\n","").trim()

But I need to get the title and description separated, so they look like this:

String title : REMOTE THE AUTOS FOR DISTRIBUTOR

String description : Download again

    
asked by anonymous 06.09.2017 / 15:56

2 answers

5

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.

    
06.09.2017 / 16:07
1

I do not know much about java , but on javascript I would use a split , with the parameter \t . Doing this:

var stringUm   = parametro.split(/\t/)[0]
var stringDois = parametro.split(/\t/)[1]

This way I would have two strings separated by the line break.

If there is a method to do split in java use it to solve your problem.

    
06.09.2017 / 16:08