# Groovy operations trim() and replace() seems to doesn't work in Soapui

**URL:** <https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112>\
**Category:** ReadyAPI\
**Tags:** readyapi, api, migrated\
**Created:** [February 19, 2018, 4:25pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112 "2018-02-19T16:25:32Z")\
**Posts on this page:** 9\
**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:** [February 19, 2018, 4:25pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112/1 "2018-02-19T16:25:32Z")

</div>

> Originally posted by **shamane2000** on February 19, 2018

I tried to get rid of whitespaces and newlines with groovy.

But if I try this in Groovy, the resul is either a not trimed string, or an error (in the case of “replace()” )

```auto
String response = " <blub>\r\nBlaaaah asd\r\n<Blä /> asd asdsad \r\n asdasd asdasd</blub>"
log.info("before trim: " + response)
log.info("After replace1: " + response.trim() )

```

Output is:

```auto
After replace1: <blub>
Blaaaah asd
<Blä /> asd asdsad
  asdasd asdasd</blub>
*/

```

and with “replace” it also not work:

```auto
log.info("After replace2: " + response.replace("[\n\r]*", "" )

```

and there just came a Error message:

```auto
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script81.groovy: 11: unexpected token: @ line 11, column 63. sponse.replace("[\n\r]*", "" ) ^ org.codehaus.groovy.syntax.SyntaxException: unexpected token: @ line 11, column 63. at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:143) at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:111) at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:237) at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:167) at org.codehaus.groovy.control.

....

*/

```

Whats wrong here?

PS: I am using ReadyAPI 2.0.2

PPS: yes, I have also tried to do it with xpath “normalize-space()”, but there I have another problem - the XML-Structure is lost… maybe I have to post another question about that…

---

<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:** [February 19, 2018, 4:53pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112/2 "2018-02-19T16:53:27Z")

</div>

> Originally posted by **avidCoder** on February 19, 2018

Is this your actual response?

String response = " \r\nBlaaaah asd\r\n\<Blä /\> asd asdsad \r\n asdasd asdasd"

Or is should look like this?

String response = " \r\nBlaaaah asd\r\n\<Blä/\>asdasdsad \r\n asdasdasdasd"

Please confirm on this.. And both replace() and replaceAll() method works.. Trying putting correct regular expressions. See below code.

```auto
String response = " <blub>\r\nBlaaaah asd\r\n<Blä /> asd asdsad \r\n asdasd asdasd</blub>"
log.info("before trim: " + response)
log.info("After replace1: " + response.trim())
log.info("After replace2: " + response.replaceAll("(\r\n)", ""))

```

This is how output look like :-

- Mon Feb 19 22:19:57 IST 2018:INFO:before trim: Blaaaah asd \<Blä /\> asd asdsad asdasd asdasd
- Mon Feb 19 22:19:57 IST 2018:INFO:After replace1: Blaaaah asd \<Blä /\> asd asdsad asdasd asdasd
- Mon Feb 19 22:19:57 IST 2018:INFO:After replace2: Blaaaah asd\<Blä /\> asd asdsad asdasd asdasd

With replace() method:-

```auto
String response = " <blub>\r\nBlaaaah asd\r\n<Blä /> asd asdsad \r\n asdasd asdasd</blub>"
log.info("before trim: " + response)
log.info("After replace1: " + response.trim())
log.info("After replace2: " + response.replace("(\r\n)", ""))

```

Output:-

- Mon Feb 19 22:26:21 IST 2018:INFO:before trim: Blaaaah asd \<Blä /\> asd asdsad asdasd asdasd
- Mon Feb 19 22:26:21 IST 2018:INFO:After replace1: Blaaaah asd \<Blä /\> asd asdsad asdasd asdasd
- Mon Feb 19 22:26:21 IST 2018:INFO:After replace2: Blaaaah asd \<Blä /\> asd asdsad asdasd asdasd

And trim() in the sense you mean to remove the white spaces inside the String :-

Like this:- Blaaaahasd\<Blä/\>asdasdsadasdasdasdasd ..

To do this you need to use replaceAll() method like this:-

```auto
String response = " <blub>\r\nBlaaaah asd\r\n<Blä /> asd asdsad \r\n asdasd asdasd</blub>"
log.info("After replace1: " + response.replaceAll("\\s+",""))

```

Output look like this:-

Mon Feb 19 22:33:48 IST 2018:INFO:After replace1: Blaaaahasd\<Blä/\>asdasdsadasdasdasdasd

Hope, it resolve your problem. 🙂

---

<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:** [February 20, 2018, 2:37pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112/3 "2018-02-20T14:37:17Z")

</div>

> Originally posted by **shamane2000** on February 20, 2018

Hi ashutoshanshu,

thanks a lot, your syntax works. I realy don’t know why I haven’t found this solution, maybe it was to late for me yesterday… 😉

Do you have also a clue, how to do this in a xpath or xqerry?

If I try this like this:

```auto
replace(/*,"\\s+","")

```

then my XML structure is away. (in a Property Transfer Step)

```auto
input:

<node>test1<subnode>test2</subnode>test3</node>

output:

test1 test2 test3

```

best regards,

Shamane2000

---

<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:** [February 20, 2018, 3:09pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112/4 "2018-02-20T15:09:54Z")

</div>

> Originally posted by **avidCoder** on February 20, 2018

You should know that replace() or replaceAll() method takes only 2 arguments within quotes like this :- replace(“”, “”) This is not the correct way of using this method :- replace(/\*,“\s+”, “”) and by the way, what is your input String and what is your expected output?

---

<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:** [February 20, 2018, 3:28pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112/5 "2018-02-20T15:28:58Z")

</div>

> Originally posted by **shamane2000** on February 20, 2018

> * * *
> 
> @ashutoshanshu wrote:
> 
> …and by the way, what is your input String and what is your expected output?
> 
> * * *

I have a lot of properties, containing xml parts (with linebreaks) and I want to linearize them for using in a \*.csv file.And save csv with aditionally linebreaks are … :smileymad:

And because all of my tries with xpath and xqerry were unsucsessfully, I trieed it now with a grovy script.

---

<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:** [February 20, 2018, 4:00pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112/6 "2018-02-20T16:00:48Z")

</div>

> Originally posted by **avidCoder** on February 20, 2018

Provide me a basic XML. I will try to linearize it.

---

<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:** [February 20, 2018, 5:37pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112/7 "2018-02-20T17:37:00Z")

</div>

> Originally posted by **shamane2000** on February 20, 2018

for example:

```auto
<node>
  test1
  <subnode>
    test2
  </subnode>
  test3
</node>

```

what I want is:

```auto
<node>test1<subnode>test2</subnode>test3</node>

```

Thanks in advance,

Shamane2000

---

<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:** [February 20, 2018, 5:51pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112/8 "2018-02-20T17:51:18Z")

</div>

> Originally posted by **avidCoder** on February 20, 2018

Oh, for this kind of stuff. You need to think for another approach. Please go through this code:-

```auto
BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\ASHUTOSH\\Desktop\\newXml.xml")));
String line;
StringBuilder sb = new StringBuilder();

while((line=br.readLine())!= null){
    sb.append(line.trim());
}

log.info sb

```

Here, newXml.xml file contains your xml data. And once you run it. You will get this result:-

Tue Feb 20 23:18:58 IST 2018:INFO:test1test2test3

---

<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:11pm UTC](https://community.smartbear.com/t/groovy-operations-trim-and-replace-seems-to-doesnt-work-in-soapui/112/9 "2026-10-07T17:11:47Z")

</div>

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

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