# How to remove/replace empty lines from string?

**URL:** <https://community.smartbear.com/t/how-to-remove-replace-empty-lines-from-string/324>\
**Category:** TestComplete\
**Tags:** test, testcomplete, migrated\
**Created:** [November 9, 2022, 12:34pm UTC](https://community.smartbear.com/t/how-to-remove-replace-empty-lines-from-string/324 "2022-11-09T12:34:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [November 9, 2022, 12:34pm UTC](https://community.smartbear.com/t/how-to-remove-replace-empty-lines-from-string/324/1 "2022-11-09T12:34:03Z")

</div>

> Originally posted by **DanNad** on November 09, 2022

Hello all

i cant get the empty lines to be removed.

Example:  
i have a String like this:  
Example:  
Object is: page.querySelectorAll(“\*”)[0].innerText  
the innerText i get is similar to this:

```auto
Test Text

Test Text

Test Text

Test Text

Test Text Test Text

```

i want the text to be this:

```auto
Test Text
Test Text
Test Text
Test Text
Test Text Test Text

```

i tried to find a way with aqString methods but i just cant find a good way to remove the empty line.

what i try to accomplish is to get all the text from a page and save it to a simple text file

Is there a way to do this?  
Help would be very much appreciated.

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [November 9, 2022, 12:48pm UTC](https://community.smartbear.com/t/how-to-remove-replace-empty-lines-from-string/324/2 "2022-11-09T12:48:34Z")

</div>

> Originally posted by **KB1** on November 09, 2022

This is what i’ve used:  
[https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/trim.html](https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/trim.html)

The trim method.

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [November 9, 2022, 1:07pm UTC](https://community.smartbear.com/t/how-to-remove-replace-empty-lines-from-string/324/3 "2022-11-09T13:07:10Z")

</div>

> Originally posted by **DanNad** on November 09, 2022

> * * *
> 
> KB1 wrote:
> 
> This is what i’ve used:  
> [https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/trim.html](https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/trim.html)
> 
> The trim method.
> 
> * * *

I tried the trim method. But it’ll remove only leading/trailing spaces

What i found is replacing blank lines via regex. e.g.

```auto
innerText.replace(/^\s*$/gim, "")

```

But i am still looking for a solution to get all the text line by line and without empty lines for the whole page object

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [November 9, 2022, 1:38pm UTC](https://community.smartbear.com/t/how-to-remove-replace-empty-lines-from-string/324/4 "2022-11-09T13:38:19Z")

</div>

> Originally posted by **rraghvani** on November 09, 2022

Have a look at [https://www.freecodecamp.org/news/javascript-split-how-to-split-a-string-into-an-array-in-js/](https://www.freecodecamp.org/news/javascript-split-how-to-split-a-string-into-an-array-in-js/)

You need to split your string into lines (array) and then remove empty lines (into a new array). This will give the output you require

Example,

 ![rraghvani_0-1668016371266.png](https://canada1.discourse-cdn.com/flex049/uploads/smartbear_prod/original/1X/87dc2f7ce38e47ff21fef8d5d6855a092da73ea4.png)

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 7, 2026, 5:15pm UTC](https://community.smartbear.com/t/how-to-remove-replace-empty-lines-from-string/324/5 "2026-10-07T17:15:48Z")

</div>

Closed on October 7, 2026, 5:15pm UTC

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