Angular ignores line break in String

Asked

Viewed 3,184 times

-3

I’m assembling a message with several messages in JAVA. After it was sent to the front/view but the angle is eating all the lines breaks:

public String salvarPorXLS(){

while (rowIterator.hasNext()) {
    pErrors += "\n"+"Atividade do processo: " + numeroProcesso + " cadastrada com sucesso"+"\n";
}

return pErrors;
}

Message:

Process activity: 00003362920178178224 successfully registered Process activity: 00003362920178178224 successfully registered Process activity: 00032906420158172001 successfully registered Process activity: 00032906420158172001 successfully registered Activity of the process: 00032906420158172001

You’re not breaking the line after the word success!

the angle is hindering the exit:

inserir a descrição da imagem aqui

  • 2

    You can’t even test this code, so there’s no way to analyze anything. But testing on ideone, modifying some variables, worked perfectly: https://ideone.com/wVMQBD

  • 1

    See if this helps you: https://answall.com/questions/7448/usando-lineNo-java

  • The angle is hindering the exit of the message

  • I will add the code in html

  • The angle is eating the line break. Java is right!

  • 3

    Then edit the question, because your doubt has nothing to do with java.

  • @jsbueno I removed the java tag pq doubt had nothing to do with java, as the author himself explained in the comments.

  • But if you thought your problem was in Java, the little piece of code that’s there is from the code in Java, Ach that makes sense. Because if so, it also has nothing to do with "angular" - the doubt is only of the output text - terminal (or text file) x html. And in fact, it has 0 of Ângular specifics in the question and answer, but besides the Java code in the question, I address Java in the answer accepted (but what I have commented on also applies to C, C#, CPP, avascript, Python and Ruby, at least: languages that use the C character escape syntax)

  • @jsbueno agree with you, the author was not able to express right his doubt in the question. The doubt, in the end, is neither of java nor of angular.

Show 4 more comments

1 answer

5


If the output is in html, then the \n is not a line break - in html, a character \n is a white space badge (whitespace) as the space (\x20) and tab (\t) .

If your output is in HTML, you should be putting each message into an appropriate semantic element - using your framework’s rules for that. If your string is going to be used as direct HTML instead of being processed by the Framework in a template, you could for example put the errors inside elements <li>...</li>, and all within a <ul>...</ul>.

Or, the "fast and cheap" way, use a <br> where are you putting \n. Another way is to keep the output as it is, and change the style (CSS) of the page presentation - use CSS so that the HTML element that will display the block of your error messages will handle \n as a line break - this can be done by placing the property white-space: pre-line; in the appropriate element, for example. ( @fernandosavio tip)

By the way, \n nothing "magic" - it’s just the linefeed character, decimal code 16 (0x10) - and the exhaust \n to represent you is inherited from the C language - among other things, the only thing you gain by writing something like "\n" + "minha frase" instead of simply "\nminha frase" is typing a lot of extra things - there’s no need for that.

  • The solution was to insert the <br> tag inside the string

  • 3

    Add another "fast and cheap" way: using the CSS property white-space.

Browser other questions tagged

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