• Beginning soapUI Scripting 2: Strings


    转自: http://testautomationnoob.blogspot.fr/2013/05/soapui-for-beginners-intro-to-strings.html

    Beginning soapUI Scripting 2: Strings

     
    You can download a "shell" project to import into soapUI and use for scripting here.

    Like my first post on scripting, the material in this post is not specific to soapUI, but strings are used so extensively I thought they were worth a brief explanation for beginners who may not be familiar with them.  Put simply, strings are text data, or a sequence of characters; "Hello World!" is an example of a string. The syntax to create a string variable is similar to the syntax we saw in my first post when creating an integer variable:

    def myVar = 3
    def myString = "Hello World!" 


    These lines assign the value 3 to the variable myVar and the string "Hello World!" to the variable myString.  Note the use of quotes, one key difference with strings-- those demarcate our text, but are not included with the actual data.  As with the integer variable myVar, once the myString variable has been created, we can reference its value in subsequent lines of our script using its name:

    log.info(myVar)
    log.info(myString)


    This code prints out the values of our variables to the log:

     

    As promised, the enclosing quotes used when the string was created are not included when the string is printed.

    Strings can be enclosed in either single quotes or double quotes in Groovy (but not in "pure" Java-- in Java, strings are always enclosed in double quotes).  You can use a mix of both when you want to include one or the other in your string.  If you want to include a single quote as part of your string, enclose the string in double quotes, and vice versa; for example:

    def stringOne = "Don't panic."
    def stringTwo = '"Look, I have double quotes!"'
    log.info(stringOne)
    log.info(stringTwo)


    This produces:

     

    You can also perform variable substitutions in Groovy; variable names preceded by the "$" character that are enclosed in strings are replaced with their underlying values.  For example:

    def myVar = 3
    def myString = "The value of myVar is $myVar"
    log.info(myString)
     

    The resulting output:

     

    One thing to remember: variable substitution only works with strings enclosed in double quotes-- in a  string enclosed in single quotes, $myVar would be interpreted as a literal (simply printed out "as is" as  "$myVar").

    Finally, while strings may behave in some ways like Java's so-called primitives (the basic data types that include integers), they are in fact objects of class String.  Consequently, they have quite a few methods at their disposal that can be used to parse and alter their contents.  There are far too many methods to cover all of them here; see the Java API entry for the String object to see the full list.  Here's some code that demonstrates a few of them, however (the lines that start with "//" in the script are comments-- they're only there for informational purposes):
    def stringOne = "   One, two, three    "
    log.info("stringOne = '$stringOne'")
    //Trim method-- removes leading and trailing whitespace from a string
    def stringTrimmed = stringOne.trim()
    log.info("Trimmed string = '$stringTrimmed'")
    //Contains method-- checks if one string is contained in another
    log.info("String 'two' in '$stringTrimmed'?")
    log.info(stringOne.contains("two"))
    //Length method-- returns the number of characters in the string
    log.info("Length of '$stringTrimmed':")
    log.info(stringTrimmed.length())
    //EndsWith method-- checks if string ends with certain characters
    log.info("'$stringTrimmed' ends with 'three'?")
    log.info(stringTrimmed.endsWith("three"))
    //StartsWith method-- checks if string starts with certain characters
    log.info("'$stringTrimmed' starts with 'Two'?")
    log.info(stringTrimmed.startsWith("Two"))
    
    And here's the output:

     

    NO COMMENTS:

     

    POST A COMMENT

    Please be respectful of others (myself included!) when posting comments. Unfortunately, I may not be able to address (or even read) all comments immediately, and I reserve the right to remove comments periodically to keep clutter to a minimum ("clean" posts that aren't disrespectful or off-topic should stay on the site for at least 30 days to give others a chance to read them). If you're looking for a solution to a particular issue, you're free to post your question here, but you may have better luck posting your question on the main forum belonging to your tool's home site (links to these are available on the navigation bar on the right).

  • 相关阅读:
    pythonday06数据类型(四)
    pythonday05数据类型(三)
    pythonday04数据类型(二)
    pythonday03数据类型(一)
    Apollo自动驾驶实践——第0讲:导论
    图论学习:生成树的Matrix-tree定理
    2020杭电多校6 Expectation
    2020牛客暑期多校第九场 B Groundhog and Apple Tree
    图论:二分图最大权匹配KM算法
    第十章 百度Apollo实战
  • 原文地址:https://www.cnblogs.com/z1500592/p/6048037.html
Copyright © 2020-2023  润新知