Replace two lines with one line

Hi,

I am working in a word document and i have to replace some texts, so i use Replace activities inside a Word Application Scope.
I have for example a new line than some text than a new line… and sometimes i need to delete that “some text” and i am ending up having a word document with like 3 new lines between some texts, so i have multiple texts and multimple lines… so this is approximetly what it’s look like:

Some text1

Some text2

Some text3

Some text4

And then if i delete for example Some text2 using the Replace activity saying Replace Some text2 with String.Empty i am getting this:

Some text1

Some text3

Some text4

And i want to look like this:

Some text1

Some text3

Some text4

Please help me, thank you.

Hi
Welcome to UiPath community
Fine but both looks same buddy
Kindly elaborate your scenario a bit more pls if possible with a sample file

Cheers @Jambor_Norbert

Yeah, sorry, the applicaion automatically deleted the empty lines, so i have this:

Some text1
\n
\n
\n
Some text3
\n
Some text4

And i want to look like this:

Some text1
\n
Some text3
\n
Some text4

Imagine that \n is not there s empty but it’s actually a new line, you get that :))

I tried everything, i looked at more that 10 topics about similar problem, but nothing worked.

Fine
Hope this expression would help you resolve this
Let the input be like this
str_input = “Some text1
\n
\n
\n
Some text3
\n
Some text4”

Now use a assign activity like this
str_output = String.Join(Environment.NewLine,str_input.Replace(Environment.Newline,””).ToString)

Hope this expression would help you
Cheers @Jambor_Norbert

1 Like

Ok, new lines can be found by using the string “^|” so if i wanted to remove all line breaks in a word doc i would do:
Capture
Please find the time to adjust this info to your needs :slight_smile:

Hello Jambor,

If that’s what you need, I’d suggest this. Replace all multiple carriage returns with two returns so you get the line in between:

he is not using text files, Word documents are very different :slight_smile:

Not working. So i need something that replaces double new lines into just one new line.

Not working. As @bcorrea said, word documents are diffrent .

It gave me this error:
Replace Text: ^| is not a valid special character for the Find What box.

You were right, actually i had \r instead of \n. Problem solved. Thank you @bcorrea

1 Like

most of the times new lines in Word are actually paragraphs and not real lines, so it can be tricky to find the right replaces :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.