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

42,156

Solution 1

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.

Solution 2

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

Might also be useful

Solution 3

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.

Solution 4

Most better way is to use pure Robot Framework keywords Remove String and Replace String both this keywords does used replace() function internally but makes your code more readable than other options. Also you may want to look at Strip String which is new in RF v 3.0

Example 1 -

${Result}=    Remove String    Hello How are you    ${SPACE}

Output 1 -

enter image description here

Example 2 -

${Result2}=   Replace String     Hello How are you    ${SPACE}        ${EMPTY}

Output 2 -
enter image description here

Note: ${SPACE}, ${EMPTY} are built in variable of robot framework.

Share:
42,156
binithb
Author by

binithb

test automation, robot framework, Python, linux, django, celery, fabric

Updated on August 27, 2020

Comments

  • binithb
    binithb over 3 years

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

    If I have a string " Hello How are you " how to convert it to "HelloHowareyou" (stripping all the white spaces)

  • binithb
    binithb over 10 years
    this offers a lot more possibilities while playing with strings - thanks
  • binithb
    binithb over 10 years
    this was what I was doing earlier, now I would rather use the way that the accepted answer recommends, thanks anyway :)
  • MarkHu
    MarkHu about 10 years
    Note that strip defaults to removing preceding/trailing spaces, but also works handily to remove enclosing quotes when you give it a parameter like yourvar.strip('"')
  • binithb
    binithb almost 10 years
    I wish to add that you can use any other python library directly in RF, like os,sys etc, its pretty useful
  • Lorenz
    Lorenz over 5 years
    This only removes spaces around it. Since Robotframework 3.0 this can also be done with Strip String in the String library. To remove all spaces from the string use the solution from Grant McCloskey.