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 "" as the character to replace with a line break e.g.

Joe BloggsAddress1Address2Address3\PostcodeCountry

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 with a new line would be:

doc.Replace("", 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 “” string in you text range. As per .net frame work string manipulation “” is preserved as “\”, because “\” is an escape character in .net frame work. So please refer the below code snippet to replace the “” character with line break in your word document.

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

Case 2: the below code snippet will replace the “” with line feed which break the single paragraph into multiple paragraph.
doc.Replace("\", "", 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 “” string in you text range. As per .net frame work string manipulation “” is preserved as “\”, because “\” is an escape character in .net frame work. So please refer the below code snippet to replace the “” character with line break in your word document.

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

Case 2: the below code snippet will replace the “” with line feed which break the single paragraph into multiple paragraph.
doc.Replace("\", "", 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 "" with a line break. If you use Environment.NewLine it replaces the "" 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("\", "\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 “” with newline character.

Code snippet:
doc.Replace("", 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("", ControlChar.LineBreak, False, False)

Please let us know if you have any other questions.

Regards,
Sathish


Loader.
Live Chat Icon For mobile
Up arrow icon