Separating Sentences in a String

Asked

Viewed 590 times

1

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

THE CASE BACK TO DISTRIBUTOR
Drop again

The code:

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

But I need to take the title and the description separately, so they stay that way:

String title :THE CASE BACK TO DISTRIBUTOR

String description: Drop again

2 answers

5


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.

1

I don’t know much about java, but in javascript I would use a split, being the parameter \t. Performing in this way:

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

That way I’d have two strings separated by line break.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.