Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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. :)

share|improve this question

2 Answers 2

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!

share|improve this answer
    
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. –  Karthik Jaganathan Apr 22 '13 at 6:54
    
Updated my answer –  Robert Lindgren Apr 22 '13 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. –  Karthik Jaganathan Apr 22 '13 at 9:02
    
Well then it starts to get really hard for you, and you will probably need multiple trimming and replacing statements. –  Robert Lindgren Apr 22 '13 at 9:12
    
Like recursively calling .Replace("> ", ">") until you have no mor ocurrencies of a ">" followed by a blank. –  Robert Lindgren Apr 22 '13 at 9:14

Try this :

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

I hope it help.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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