Problem Breaking a String

Asked

Viewed 6,938 times

1

I have a string that is concatenated inside a loop, wanted that for each loop a new line was created with the concatenated values.

Follow the code I’m using:

mensagem ="";

for (int i = 0; i < titulosReceber.Rows.Count; i++) 
    mensagem += "Titulos: " + (titulosReceber[i].TitId).ToString() +
    "FormaPagto: " + (titulosReceber[i].NomeMeioPagamento).ToString() +
    "Valor: " + (titulosReceber[i].Valor).ToString() + " Dt. Venc: " +
    (titulosReceber[i].dVenc)+"\n" ;

SalvarHistorico("Ação Faturar da Saida, gerado(s) o(s)"+ mensagem,
Historico.enumHistoricoTipo.SAI_Faturar, this.Saida[0].Id, IdOperador );

To string is concatenated, but does not break the line.

  • 3

    Where is this being used? The line break is present yes, it cannot be. But depending on where it is used, it will not appear because \n does not work in all contexts, such as HTML, for example. Line breaking in HTML is <br />.

  • You want the SalvarHistorico be called to each iteration?

  • Andre, if power just breaks the string it helps me enough.

  • What @bigown said is correct, \n does not work on web pages, you should give a replace \n for <br />, must have some methods/mechanism to execute this replace

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

3

Line breaking is normally occurring through the \n there is no way not to have according to this code.

Your problem is elsewhere. When will you present this string generated, you are not seeing the text break by the way the mechanism that will make the presentation manipulates it. That is, when you print, the \n is not visible as a line break. As you indicate using ASP.NET, you may be trying to display this on an HTML page, whose line break is represented by <br />. Then either you change this code to use this text as a break or when you mount the page converts the \n for <br />.

0

Where are you displaying this generated message? If it is in a context HTML, you can use mensagem.Replace(Environment.NewLine, "<br />") before showing that it should work. Another option would be to put the \n direct place the <br />.

  • Ola Richard days, thanks for the help, already manage to resolve the issue follows the code.

  • Mark an answer as correct then

0


StringBuilder mensagem = new StringBuilder(); 

for (int i = 0; i < titulosReceber.Rows.Count; i++)
    mensagem.Append(string.Format("<br/><b>Titulos:</b> {0} <b>Forma Pagto: :</b>{1} <b>Valor :</b>{2} <b>Dt. Venc :</b>{3}", titulosReceber[i].TitId, titulosReceber[i].NomeMeioPagamento, titulosReceber[i].Valor, titulosReceber[i].dVenc.ToString("dd/MM/yyyy")).ToString());

    SalvarHistorico("Ação Faturar da Saida, gerado(s) o(s)"+ mensagem, Historico.enumHistoricoTipo.SAI_Faturar, this.Saida[0].Id, IdOperador );

Browser other questions tagged

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