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

冷暖自知 提交于 2019-12-10 12:59:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!