2

I have a variable str which is having some text. I just show this text on the my UI. But I have a condition, suppose this variable is having 5oo words, so I need to put a
tag after every 50 words.

How can I do that?

below is my c# code through which I am sending a mail as html

Emailbody += " <tr>";
Emailbody += " <td align='left' valign='Top' nowrap><span class=style17>Purpose of     travel</span></td>";
Emailbody += "<td align='center' valign='Top'>:</td>";
Emailbody += " <td align='left' valign='Top'><span class=style17>&nbsp;&nbsp;" + TextBox1.Text +     "</span></td>";
Emailbody += " <td>&nbsp;</td>";
Emailbody += "  <td align='left' nowrap'><span class=style17>Advance</span></td>";
Emailbody += " <td align='center'>:</td>";
Emailbody += "<td align='left' nowrap><span class=style17>"+TextBox2.Text+"</td>";
Emailbody += " </tr>";

I need the the solution for mt TextBox1.Text

Gaurav
  • 557
  • 4
  • 11
  • 28
  • 1
    Show us some codes please of what have you done. – Mark May 15 '13 at 05:23
  • Possible duplicate of [Word wrap a a string in multiple lines](http://stackoverflow.com/q/3961278/254830) – doppelgreener May 15 '13 at 05:44
  • 3
    Side note: this sample shows how *not to* construct HTML: script injection and invalid HTML due to incorrectly encoded values, some not closed tags. In addition strange mix of inline and CSS styles for no particular reason, tables for layout may not be considered best approach by some. – Alexei Levenkov May 15 '13 at 05:48

3 Answers3

2

If you are using C#, then you can do so like :

    public string SplitLine(string input)
    {
        var wordList = input.Split(' ');
        var sb = new StringBuilder();
        for (int index = 0; index < wordList.Length; index++)
        {
            if(index % 50 == 0 && index > 0)
                sb.Append("<br/>" + wordList[index]);
            else
                sb.Append(wordList[index] + ' ');
        }
        return sb.ToString();
    }
cvraman
  • 1,687
  • 1
  • 12
  • 18
0

Use javascript as follows:

var count=0;
for(var i=0;i<str.length;i++)
{


  if((str[i])=="")
  {
    count++;
    if(count==50)
    {
      str[i]="<br/>"
      count=0;
    }
  }

}

Hope its helpful.

Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • 1
    He's asking for a new line after 50 words and not after x characters and why do you use count instead of i? – Chief Wiggum May 15 '13 at 05:31
  • he has put "suppose", means just for giving example to us he has mentioned that 50 characters stuff. – Freelancer May 15 '13 at 05:32
  • 1
    Are you supposed to be setting count to 0 when you insert the newline? Also is `str[count] = "
    "` (it's `
    ` or `
    `, not ``) an attempt to insert a line break into a string? That doesn't seem to work - [perhaps this could be of use](http://stackoverflow.com/q/4313841/254830).
    – doppelgreener May 15 '13 at 05:41
0

I assume you want to do that in c#?

Have a look at possible duplicate. The given function allows you do specify rather than a number of characters or words a line width in pixels and with the font and font size it calculates a proper length. You have to adapt it to your HTML output, but the rest stays the same.

You can use the List then like this:

List<string> lines = WrapText(TextBox1.Text, 300, "Calibri", 11);

string longText = string.Empty

foreach (var item in lines)
{
    longText += item + "</br>";
}

Emailbody += " <tr>";
Emailbody += " <td align='left' valign='Top' nowrap><span class=style17>Purpose of travel</span></td>";
Emailbody += "<td align='center' valign='Top'>:</td>";
Emailbody += " <td align='left' valign='Top'><span class=style17>" + longText + "</span></td>";
Emailbody += " <td>&nbsp;</td>";
Emailbody += "  <td align='left' nowrap'><span class=style17>Advance</span></td>";
Emailbody += " <td align='center'>:</td>";
Emailbody += "<td align='left' nowrap><span class=style17>"+TextBox2.Text+"</td>";
Emailbody += " </tr>";
Community
  • 1
  • 1
Chief Wiggum
  • 2,784
  • 2
  • 31
  • 44
  • 3
    You should not repost the same answer under your name - simple "possible duplicate" comment would be enough. – Alexei Levenkov May 15 '13 at 05:43
  • FormattedText comes under web.Mvc namespace but it is not showing in my cs file, instead showing error you are missing assembly reference – Gaurav May 15 '13 at 05:43
  • 1
    I agree with @Alexei. Furthermore if you _are_ going to post someone else's work, at least explicitly credit the author and indicate this was not your work. – doppelgreener May 15 '13 at 05:45
  • you are no one to remark. You have to report/vote as duplicate – Freelancer May 15 '13 at 05:49
  • Now when you make this code to actually count words in text (i.e. exclude HTML tags properly) it would look like good solution. – Alexei Levenkov May 15 '13 at 05:51
  • I am very new to this forum. I don't know all rules of this forum, it'll take time to be habitual all rules. Sorry for sorry for inconvenience – Gaurav May 15 '13 at 05:51
  • @Gaurav Take your time, most of us didn't pick it up immediately. Also, bear in mind it isn't a forum. ;) Have you checked the About and FAQ pages out yet? – doppelgreener May 15 '13 at 05:53
  • @AlexeiLevenkov: I assume the string is not the given HTML but rather the content of his TextField as he mentions at the end of his question: "I need the the solution for mt TextBox1.Text" – Chief Wiggum May 15 '13 at 05:53
  • But so far the content of that `TextBox1.Text` used as raw HTML - than you should show how to *correctly* encode that text after measuring words so it matches your suggestion. – Alexei Levenkov May 15 '13 at 05:55