How to trim or strip white spaces from a String while using Robot Framework
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 -
Example 2 -
${Result2}= Replace String Hello How are you ${SPACE} ${EMPTY}
Note: ${SPACE}, ${EMPTY} are built in variable of robot framework.
binithb
test automation, robot framework, Python, linux, django, celery, fabric
Updated on August 27, 2020Comments
-
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 over 10 yearsthis offers a lot more possibilities while playing with strings - thanks
-
binithb over 10 yearsthis was what I was doing earlier, now I would rather use the way that the accepted answer recommends, thanks anyway :)
-
MarkHu about 10 yearsNote 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 almost 10 yearsI wish to add that you can use any other python library directly in RF, like os,sys etc, its pretty useful
-
Lorenz over 5 yearsThis 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.