Skip to main content
Kat
Employee
Jul 31, 2023 11:50 AM UTC
Solved

How to TRIM Output/Buffer Value

I want to trim the Left and Right of text whitespaces from value of buffer/output, how can I do trimming in Text ?

 

[Originally asked and solved on Support Hub]

Community Manager
Jul 31, 2023 11:51 AM UTC

You can use the following syntax to remove spaces at either end of your string. Assuming the you have the string stored in a buffer variable called "Variable" :

{CALC[TRIM("{B[Variable]}")]}

 

Use the TC Set Buffer generic module in your test case as follows:

For the value of the Sub attribute "Buffer" use the same variable name i.e. Variable

For the value of the sub attribute "Value" use the syntax given above i.e. {CALC[TRIM("{B[Variable]}")]}

 

The result of the execution will be stored in the buffer variable "Variable"

14 replies

Kat
KatEmployeeAuthor
Employee
Jul 31, 2023 11:55 AM UTC

Even if this question is very old, I want to point to a simpler solution than all the advice above: Use REGEX

 

https://documentation.tricentis.com/tosca/1300/en/content/tbox/regular_expressions.htm#Extractingtextusingregularexpressions

 

In your case:

{Regex["[\s]*(?<stringbuffer>[a-z|\d]*)[\s]*"]}

should store the string you are looking for into buffer stringbuffer.

 

This feature deserves to be known better.

Quality Matters, Kat
Community Manager
Jul 31, 2023 11:56 AM UTC

A very simple way is to work with verify Buffer:

(in front of and behind the xb should be a blank!)

 

91952eaadbd3c0584d5d2b24ca9619d4.iix

 

Start: " Hello World "

Result: "Hello World"

Community Manager
Jul 31, 2023 11:56 AM UTC

Quick update as this very old questions is still one of the most viewed content:

With Tosca 14.2 the String operations {TRIM} was introduced. This would be the perfect solution. With the optional Parameter [START] or [END] only the whitespaces at the beginning or end are trimmed.

{TRIM[InputText][Optional Parameter]}

e.g.: {TRIM[{B[Buffer1]}]}

Kat
KatEmployeeAuthor
Employee
Jul 31, 2023 11:59 AM UTC

You can simply use {TRIM[{B[value_to_call_buffer]}]}, TRIM attribute is going to trim all leading and trailing white spaces,

Quality Matters, Kat