0

I am using a MultiLine Rich Text box in my custom list, now I want to remove leading and trailing spaces in the MultiLine Rich text box's input.

I have written an List item event receiver with string.Trim() function and its not working on Rich Text box.

In the code, when I placed a debugger, I am getting the following values from the multiline text box. Below code is just an example, I may also apply Bold, color, etc to my text, accordingly the HTML code may vary.

"<html><div>&nbsp;&nbsp;sample&nbsp; Text&nbsp;&nbsp; </div></html>"

Now how to remove the leading and trailing spaces from MultiLine Rich text box.??

Thanks in advace. :)

2 Answers 2

1

You could probably first decode the string from html, then trim it and then reencode it to html, like this:

var aString = yourTextBoxText;

aString= SPEncode.HtmlDecode(aString);

Since the texbox will always render <html><div> before the content

you can not use .Trim(), instead use:

aString = aString.Replace("<html> <div>", "");
aString = aString.Replace("</div> </html>", "");

and then

aString = aString.Trim();

and re add your html tags:

aString = "<html> <div>" + aString + "</div> </html>"

and encode:

aString = SPEncode.HtmlEncode(aString);

Quite extensive, would be great if there where som easier way!

5
  • Hi @Robert Lindgren, thanks for your reply. After HtmlDecode, I'm getting the following in the aString. <html> <div> Sample Test </div> </html>. I want to remove spaces before "Sample" and after "Test". Thanks in advace. Apr 22, 2013 at 6:54
  • Updated my answer Apr 22, 2013 at 7:03
  • Hi @Robert Lindgren, thanks for your code and reply. As my List field is Rich Text box, I will be able to edit my text like applying color, bolding it, etc.. Now I applied Bold, color and underline to my text in the RichTextbox field and I got the below values after Htmlencodein the code. <html> <div> <div><strong><em><font color="#99cc00"><u> fds </u> </font></em></strong></div></div> </html>.. I need a generalized code to remove spaces. Apr 22, 2013 at 9:02
  • Well then it starts to get really hard for you, and you will probably need multiple trimming and replacing statements. Apr 22, 2013 at 9:12
  • Like recursively calling .Replace("> ", ">") until you have no mor ocurrencies of a ">" followed by a blank. Apr 22, 2013 at 9:14
0

Try this :

 string trimtext = Regex.Replace(text, @"s+", " ").Trim();

I hope it help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.