We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Replacing special character with line break / new line

Thread ID:

Created:

Updated:

Platform:

Replies:

119774 Jul 30,2015 09:09 AM UTC Jul 31,2015 12:34 PM UTC ASP.NET Web Forms 4
loading
Tags: DocIO
Mark Walker
Asked On July 30, 2015 09:09 AM UTC

Hi all

Just for anyone else out there that might have this problem. We use DocIO to generate template word docs for clients and replace certain keywords with our own values. We had a problem with an address block that contains a "\n" as the character to replace with a line break e.g.

Joe Bloggs\nAddress1\nAddress2\nAddress3\Postcode\nCountry

We needed the above to read as

Joe Blogs
Address1
Address2
Address3
Postcode
Country

Using the replace function with the BreakType.LineBreak didn't work but using Environment.NewLine (we're using VB.NET) does. So the code to replace \n with a new line would be:

doc.Replace("\n", Environment.NewLine, False, False)

Thought this might help anyone else who's having a similar issue.

Cheers

Sarathkumar V [Syncfusion]
Replied On July 30, 2015 02:36 PM UTC

Hi Cheers,

Thank you for using Syncfusion products.

On further analyzing your requirement, we found that you have “\n” string in you text range. As per .net frame work string manipulation “\n” is preserved as “\\n”, because “\” is an escape character in .net frame work. So please refer the below code snippet to replace the “\n” character with line break in your word document.

Case 1: the below code snippet will replace the “\n” with line break which break the single paragraph into multiple line.
doc.Replace("\\n", "\v", false, false);

Case 2: the below code snippet will replace the “\n” with line feed which break the single paragraph into multiple paragraph.
doc.Replace("\\n", "\n", false, false);

Please let us know if you have any other questions.

Regards,
Sarath

Sarathkumar V [Syncfusion]
Replied On July 30, 2015 02:38 PM UTC

Hi Mark,

Kindly ignore the previous update.

On further analyzing your requirement, we found that you have “\n” string in you text range. As per .net frame work string manipulation “\n” is preserved as “\\n”, because “\” is an escape character in .net frame work. So please refer the below code snippet to replace the “\n” character with line break in your word document.

Case 1: the below code snippet will replace the “\n” with line break which break the single paragraph into multiple line.
doc.Replace("\\n", "\v", false, false);

Case 2: the below code snippet will replace the “\n” with line feed which break the single paragraph into multiple paragraph.
doc.Replace("\\n", "\n", false, false);

Please let us know if you have any other questions.

Regards,
Sarath

Mark Walker
Replied On July 30, 2015 02:50 PM UTC

Yeah, that doesn't work for replacing "\n" with a line break. If you use Environment.NewLine it replaces the "\n" text with a new line a works perfectly. Just thought I'd let anyone else out there with the same problem know how to fix it.

Cheers

Sathish K [Syncfusion]
Replied On July 31, 2015 12:34 PM UTC

Hi Mark,

Thank you for your update.

The mentioned replacement code “
doc.Replace("\\n", "\v", false, false)is working fine in C# and it is not working in VB since we can’t use backslash(\) to escape characters in VB. As you suggested you shall make use of Environment.NewLine” or “ControlChar.CrLfof DocIO to replace the string “\n” with newline character.

Code snippet:
doc.Replace("\n", ControlChar.CrLf, False, False)

If you want to insert line break instead of newline character, then you shall make use of below code.
doc.Replace("\n", ControlChar.LineBreak, False, False)

Please let us know if you have any other questions.

Regards,
Sathish


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;