问题
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)
回答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.
回答2:
${time_stamp}= Get Time
${time_stamp}= Evaluate '${time_stamp}'.replace(' ','_')
Might also be useful
回答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.
来源:https://stackoverflow.com/questions/17832094/how-to-trim-or-strip-white-spaces-from-a-string-while-using-robot-framework