How to trim or strip white spaces from a String while using Robot Framework

Also ${str.strip()} works. It uses the Extended variable syntax Example:

*** Variables ***
${str}=    ${SPACE}${SPACE}${SPACE}foo${SPACE}${SPACE}${SPACE}

*** Test cases ***
print string
    log    ${str}         # will be printed with spaces around it
    log    ${str.strip()} # will be printed without spaces around it

Run with pybot -L TRACE to see what is being passed to log keyword.


You can do this using a python function, or using regular expressions.

MyLibrary.py

def Remove_Whitespace(instring):
    return instring.strip()

MySuite.txt

| *Setting* | *Value* |
| Library   | String        
| Library   | ./MyLibrary.py

| *Test Case* | *Action* | *Argument*
| T100 | [Documentation] | Removes leading and trailing whitespace from a string.
       # whatever you need to do to get ${myString}
|      | ${tmp}= | Remove Whitespace | ${myString}
       # ${tmp} is ${myString} with the leading and trailing whitespace removed.
| T101 | [Documentation] | Removes leading and trailing whitespace from a string.
       # whatever you need to do to get ${myString}
       # The \ is needed to create an empty string in this format
|      | ${tmp}= | Replace String Using Regexp | ${myString} | (^[ ]+|[ ]+$) | \
       # ${tmp} is ${myString} with the leading and trailing whitespace removed.

${time_stamp}=       Get Time
${time_stamp}=       Evaluate    '${time_stamp}'.replace(' ','_')

Might also be useful