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. Image for the cookie policy date

Replacing special character with line break / new line

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

4 Replies

SV Sarathkumar V Syncfusion Team 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


SV Sarathkumar V Syncfusion Team 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


DG Damian Guy 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


SK Sathish K Syncfusion Team 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


Loader.
Up arrow icon