如何在使用Robot Framework时从String中修剪或去除空格

时间:2013-07-24 11:02:13

标签: automated-tests robotframework

如何在使用Robot Framework

时修剪或剥离字符串中的空格

如果我有一个字符串“你好,你好吗” 如何将其转换为“HelloHowareyou”(剥离所有空格)

4 个答案:

答案 0 :(得分:17)

$ {str.strip()}也有效。它使用Extended variable syntax示例:

*** 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

使用pybot -L TRACE运行以查看传递给log关键字的内容。

答案 1 :(得分:4)

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

也可能有用

答案 2 :(得分:1)

您可以使用python函数或使用正则表达式来执行此操作。

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.

答案 3 :(得分:0)

最好的方法是使用纯Robot Robot关键字Remove StringReplace String,这两个关键字的确在内部使用了replace()功能,但是比其他选项更具可读性。另外,您可能想看看Strip String,它是RF v 3.0中的新功能

示例1 -

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

输出1-

enter image description here

示例2 -

{{1}}

输出2-
enter image description here

注意:${SPACE}, ${EMPTY}是内置在机器人框架的变量中。