We're sorry Aspose doesn't work properply without JavaScript enabled.

# Replace multiple line text with a single line text

Example text:

@gkumar16 If lines are separated by a paragraph break, you should use special metacharacter &p in your search pattern:

Document doc = new Document("C:\\Temp\\in.docx");
doc.save("C:\\Temp\\out.docx");


in.docx (12.4 KB)
out.docx (9.8 KB)

If the lines are separated by soft line break (Shift+Enter) then you should &l metacharacter.
https://reference.aspose.com/words/java/com.aspose.words/range/#replace-java.lang.String-java.lang.String

Thanks @alexey.noskov. It worked.

@alexey.noskov
I have one another use case where I don’t know where the line break is. Is it possible to search and replace in that case?

@gkumar16 You can use regular expression. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getRange().replace(regex, "one line");
doc.save("C:\\Temp\\out.docx");


in.docx (12.5 KB)
out.docx (10.0 KB)

In the regular expression the following special symbols are used:

• &p - Paragraph break;
• \\s+ - one or more whitespaces;
• \u000b - soft line break.

@alexey.noskov
The address will not be properly line break like above.
For example,
Apt. 887 7977 Guillermo Brook, New
Yaekoport, ME 93650

or no comma at the end of line
Apt. 887 7977 Guillermo Brook
New Yaekoport, ME 93650