官方文档:http://robotframework.org/robotframework/latest/libraries/String.html

Introduction

A test library for string manipulation and verification.String is Robot Framework's standard library for manipulating strings

Following keywords from BuiltIn library can also be used with strings:

  • Catenate
  • Get Length
  • Length Should Be
  • Should (Not) Be Empty
  • Should (Not) Be Equal (As Strings/Integers/Numbers)
  • Should (Not) Match (Regexp)
  • Should (Not) Contain
  • Should (Not) Start With
  • Should (Not) End With
  • Convert To String
  • Convert To Bytes

Altogether 30 keywords.

Keyword Arguments Documentation
Convert To Lowercase string

Converts string to lowercase.

Examples:

${str1} = Convert To Lowercase ABC
${str2} = Convert To Lowercase 1A2c3D
Should Be Equal ${str1} abc
Should Be Equal ${str2} 1a2c3d

New in Robot Framework 2.8.6.

Convert To Uppercase string

Converts string to uppercase.

Examples:

${str1} = Convert To Uppercase abc
${str2} = Convert To Uppercase 1a2C3d
Should Be Equal ${str1} ABC
Should Be Equal ${str2} 1A2C3D

New in Robot Framework 2.8.6.

Decode Bytes To String bytes, encoding,errors=strict

Decodes the given bytes to a Unicode string using the given encoding.

errors argument controls what to do if decoding some bytes fails. All values accepted by decode method in Python are valid, but in practice the following values are most useful:

  • strict: fail if characters cannot be decoded (default)
  • ignore: ignore characters that cannot be decoded
  • replace: replace characters that cannot be decoded with a replacement character

Examples:

${string} = Decode Bytes To String ${bytes} UTF-8  
${string} = Decode Bytes To String ${bytes} ASCII errors=ignore

Use Encode String To Bytes if you need to convert Unicode strings to byte strings, and Convert To String in BuiltIn if you need to convert arbitrary objects to Unicode strings.

Encode String To Bytes string, encoding,errors=strict

Encodes the given Unicode string to bytes using the given encoding.

errors argument controls what to do if encoding some characters fails. All values accepted by encode method in Python are valid, but in practice the following values are most useful:

  • strict: fail if characters cannot be encoded (default)
  • ignore: ignore characters that cannot be encoded
  • replace: replace characters that cannot be encoded with a replacement character

Examples:

${bytes} = Encode String To Bytes ${string} UTF-8  
${bytes} = Encode String To Bytes ${string} ASCII errors=ignore

Use Convert To Bytes in BuiltIn if you want to create bytes based on character or integer sequences. Use Decode Bytes To String if you need to convert byte strings to Unicode strings and Convert To String in BuiltIn if you need to convert arbitrary objects to Unicode.

Fetch From Left string, marker

Returns contents of the string before the first occurrence of marker.

If the marker is not found, whole string is returned.

See also Fetch From RightSplit String and Split String From Right.

Fetch From Right string, marker

Returns contents of the string after the last occurrence of marker.

If the marker is not found, whole string is returned.

See also Fetch From LeftSplit String and Split String From Right.

Generate Random String length=8, chars=[LETTERS][NUMBERS]

Generates(生成) a string with a desired(期望) length from the given chars.

The population sequence chars contains the characters to use when generating the random string. It can contain any characters, and it is possible to use special markers explained in the table below:

Marker Explanation
[LOWER] Lowercase ASCII characters from a to z.
[UPPER] Uppercase ASCII characters from A to Z.
[LETTERS] Lowercase and uppercase ASCII characters.
[NUMBERS] Numbers from 0 to 9.

Examples:

log:

20170719 11:20:59.200 : INFO : ${ret} = bjgo5Rz7
20170719 11:20:59.200 : INFO : ${low} = ourdecxrjuej
20170719 11:20:59.200 : INFO : ${up} = ULZYTGLIEISI
20170719 11:20:59.200 : INFO : ${LETT} = HUuBavRQsTKx
20170719 11:20:59.200 : INFO : ${NUM} = 823509837958
20170719 11:20:59.200 : INFO : ${bin} = 01010001
20170719 11:20:59.200 : INFO : ${hex} = 2154

Get Line string, line_number

Returns the specified line from the given string.

Line numbering starts from 0 and it is possible to use negative indices(负数) to refer to lines from the end. The line is returned without the newline character.

Examples:

${first} = Get Line ${string} 0
${2nd last} = Get Line ${string} -2

Use Split To Lines if all lines are needed.

Get Line Count string

Returns and logs the number of lines in the given string.

Get Lines Containing String string, pattern,case_insensitive=False

Returns lines of the given string that contain the pattern.

The pattern is always considered to be a normal string, not a glob or regexp pattern. A line matches if the pattern is found anywhere on it.

The match is case-sensitive by default, but giving case_insensitive a true value makes it case-insensitive. The value is considered true if it is a non-empty string that is not equal to false or no. If the value is not a string, its truth value is got directly in Python.

Lines are returned as one string catenated back together with newlines. Possible trailing newline is never returned. The number of matching lines is automatically logged.

Examples:

${lines} = Get Lines Containing String ${result} An example  
${ret} = Get Lines Containing String ${ret} FAIL case-insensitive

See Get Lines Matching Pattern and Get Lines Matching Regexp if you need more complex pattern matching.

Get Lines Matching Pattern string, pattern,case_insensitive=False

Returns lines of the given string that match the pattern.

The pattern is a glob pattern where:

* matches everything
? matches any single character
[chars] matches any character inside square brackets (e.g. [abc] matches either ab or c)
[!chars] matches any character not inside square brackets

A line matches only if it matches the pattern fully.

The match is case-sensitive by default, but giving case_insensitive a true value makes it case-insensitive. The value is considered true if it is a non-empty string that is not equal to false or no. If the value is not a string, its truth value is got directly in Python.

Lines are returned as one string catenated back together with newlines. Possible trailing newline is never returned. The number of matching lines is automatically logged.

Examples:

${lines} = Get Lines Matching Pattern ${result} Wild???? example  
${ret} = Get Lines Matching Pattern ${ret} FAIL: * case_insensitive=true

See Get Lines Matching Regexp if you need more complex patterns and Get Lines Containing String if searching literal strings is enough.

Get Lines Matching Regexp string, pattern,partial_match=False

Returns lines of the given string that match the regexp pattern.

See BuiltIn.Should Match Regexp for more information about Python regular expression syntax in general and how to use it in Robot Framework test data in particular.

By default lines match only if they match the pattern fully, but partial matching can be enabled by giving the partial_match argument a true value. The value is considered true if it is a non-empty string that is not equal to false or no. If the value is not a string, its truth value is got directly in Python.

If the pattern is empty, it matches only empty lines by default. When partial matching is enabled, empty pattern matches all lines.

Notice that to make the match case-insensitive, you need to prefix the pattern with case-insensitive flag (?i).

Lines are returned as one string concatenated back together with newlines. Possible trailing newline is never returned. The number of matching lines is automatically logged.

Examples:

${lines} = Get Lines Matching Regexp ${result} Reg\\w{3} example  
${lines} = Get Lines Matching Regexp ${result} Reg\\w{3} example partial_match=true
${ret} = Get Lines Matching Regexp ${ret} (?i)FAIL: .*  

See Get Lines Matching Pattern and Get Lines Containing String if you do not need full regular expression powers (and complexity).

partial_match argument is new in Robot Framework 2.9. In earlier versions exact match was always required.

Get Regexp Matches string, pattern,*groups

Returns a list of all non-overlapping matches in the given string.

string is the string to find matches from and pattern is the regular expression. See BuiltIn.Should Match Regexp for more information about Python regular expression syntax in general and how to use it in Robot Framework test data in particular.

If no groups are used, the returned list contains full matches. If one group is used, the list contains only contents of that group. If multiple groups are used, the list contains tuples that contain individual group contents. All groups can be given as indexes (starting from 1) and named groups also as names.

Examples:

${no match} = Get Regexp Matches the string xxx    
${matches} = Get Regexp Matches the string t..    
${one group} = Get Regexp Matches the string t(..) 1  
${named group} = Get Regexp Matches the string t(?P<name>..) name  
${two groups} = Get Regexp Matches the string t(.)(.) 1 2

=>

${no match} = []
${matches} = ['the', 'tri']
${one group} = ['he', 'ri']
${named group} = ['he', 'ri']
${two groups} = [('h', 'e'), ('r', 'i')]

New in Robot Framework 2.9.

Get Substring string, start,end=None

Returns a substring from start index to end index.可截取到想要的自字符串

The start index is inclusive and end is exclusive. Indexing starts from 0, and it is possible to use negative indices to refer to characters from the end.

Examples:

${ignore first} = Get Substring ${string} 1  
${ignore last} = Get Substring ${string}   -1
${5th to 10th} = Get Substring ${string} 4 10
${first two} = Get Substring ${string}   1
${last two} = Get Substring ${string} -2  
Remove String string, *removables

Removes all removables from the given string.移除掉字符串中给定的字符

removables are used as literal strings. Each removable will be matched to a temporary string from which preceding removables have been already removed. See second example below.

Use Remove String Using Regexp if more powerful pattern matching is needed. If only a certain number of matches should be removed, Replace String or Replace String Using Regexp can be used.

A modified version of the string is returned and the original string is not altered.

Examples:

${str} = Remove String Robot Framework work  
Should Be Equal ${str} Robot Frame    
${str} = Remove String Robot Framework o bt
Should Be Equal ${str} R Framewrk    

New in Robot Framework 2.8.2.

Remove String Using Regexp string, *patterns

Removes patterns from the given string.

This keyword is otherwise identical to Remove String, but the patterns to search for are considered to be a regular expression. See Replace String Using Regexp for more information about the regular expression syntax. That keyword can also be used if there is a need to remove only a certain number of occurrences.

New in Robot Framework 2.8.2.

Replace String string, search_for,replace_with,count=-1

Replaces search_for in the given string with replace_with.

search_for is used as a literal string. See Replace String Using Regexp if more powerful pattern matching is needed. If you need to just remove a string see Remove String.

If the optional argument count is given, only that many occurrences from left are replaced. Negative count means that all occurrences are replaced (default behaviour) and zero means that nothing is done.

A modified version of the string is returned and the original string is not altered.

Examples:

${str} = Replace String Hello, world! world tellus  
Should Be Equal ${str} Hello, tellus!      
${str} = Replace String Hello, world! l ${EMPTY} count=1
Should Be Equal ${str} Helo, world!      
Replace String Using Regexp string, pattern,replace_with,count=-1

Replaces pattern in the given string with replace_with.

This keyword is otherwise identical to Replace String, but the pattern to search for is considered to be a regular expression. See BuiltIn.Should Match Regexp for more information about Python regular expression syntax in general and how to use it in Robot Framework test data in particular.

If you need to just remove a string see Remove String Using Regexp.

Examples:

${str} = Replace String Using Regexp ${str} 20\\d\\d-\\d\\d-\\d\\d <DATE>  
${str} = Replace String Using Regexp ${str} (Hello|Hi) ${EMPTY} count=1
Should Be Byte String item, msg=None

Fails if the given item is not a byte string.

Use Should Be Unicode String if you want to verify the item is a Unicode string, or Should Be String if both Unicode and byte strings are fine. See Should Be String for more details about Unicode strings and byte strings.

The default error message can be overridden with the optional msg argument.

Should Be Lowercase string, msg=None

Fails if the given string is not in lowercase.

For example, 'string' and 'with specials!' would pass, and 'String''' and ' ' would fail.

The default error message can be overridden with the optional msg argument.

See also Should Be Uppercase and Should Be Titlecase.

Should Be String item, msg=None

Fails if the given item is not a string.

With Python 2, except with IronPython, this keyword passes regardless is the item a Unicode string or a byte string. Use Should Be Unicode String or Should Be Byte String if you want to restrict the string type. Notice that with Python 2, except with IronPython, 'string' creates a byte string and u'unicode' must be used to create a Unicode string.

With Python 3 and IronPython, this keyword passes if the string is a Unicode string but fails if it is bytes. Notice that with both Python 3 and IronPython, 'string' creates a Unicode string, and b'bytes' must be used to create a byte string.

The default error message can be overridden with the optional msg argument.

Should Be Titlecase string, msg=None

Fails if given string is not title.

string is a titlecased string if there is at least one character in it, uppercase characters only follow uncased characters and lowercase characters only cased ones.

For example, 'This Is Title' would pass, and 'Word In UPPER''Word In lower''' and ' ' would fail.

The default error message can be overridden with the optional msg argument.

See also Should Be Uppercase and Should Be Lowercase.

Should Be Unicode String item, msg=None

Fails if the given item is not a Unicode string.

Use Should Be Byte String if you want to verify the item is a byte string, or Should Be String if both Unicode and byte strings are fine. See Should Be String for more details about Unicode strings and byte strings.

The default error message can be overridden with the optional msg argument.

Should Be Uppercase string, msg=None

Fails if the given string is not in uppercase.

For example, 'STRING' and 'WITH SPECIALS!' would pass, and 'String''' and ' ' would fail.

The default error message can be overridden with the optional msg argument.

See also Should Be Titlecase and Should Be Lowercase.

Should Not Be String item, msg=None

Fails if the given item is a string.

See Should Be String for more details about Unicode strings and byte strings.

The default error message can be overridden with the optional msg argument.

Split String string,separator=None,max_split=-1

Splits the string using separator as a delimiter string.

If a separator is not given, any whitespace string is a separator. In that case also possible consecutive whitespace as well as leading and trailing whitespace is ignored.

Split words are returned as a list. If the optional max_split is given, at most max_split splits are done, and the returned list will have maximum max_split + 1 elements.

Examples:

@{words} = Split String ${string}      
@{words} = Split String ${string} ,${SPACE}    
${pre} ${post} = Split String ${string} :: 1

See Split String From Right if you want to start splitting from right, and Fetch From Left and Fetch From Right if you only want to get first/last part of the string.

Split String From Right string,separator=None,max_split=-1

Splits the string using separator starting from right.

Same as Split String, but splitting is started from right. This has an effect only when max_split is given.

Examples:

${first} ${rest} = Split String ${string} - 1
${rest} ${last} = Split String From Right ${string} - 1
Split String To Characters string

Splits the given string to characters.

Example:

@{characters} = Split String To Characters ${string}
Split To Lines string, start=0,end=None

Splits the given string to lines.

It is possible to get only a selection of lines from start to end so that start index is inclusive and end is exclusive. Line numbering starts from 0, and it is possible to use negative indices to refer to lines from the end.

Lines are returned without the newlines. The number of returned lines is automatically logged.

Examples:

@{lines} = Split To Lines ${manylines}    
@{ignore first} = Split To Lines ${manylines} 1  
@{ignore last} = Split To Lines ${manylines}   -1
@{5th to 10th} = Split To Lines ${manylines} 4 10
@{first two} = Split To Lines ${manylines}   1
@{last two} = Split To Lines ${manylines} -2  

Use Get Line if you only need to get a single line.

Strip String string, mode=both,characters=None

Remove leading and/or trailing whitespaces from the given string.

mode is either left to remove leading characters, right to remove trailing characters, both (default) to remove the characters from both sides of the string or none to return the unmodified string.

If the optional characters is given, it must be a string and the characters in the string will be stripped in the string. Please note, that this is not a substring to be removed but a list of characters, see the example below.

Examples:

${stripped}= Strip String ${SPACE}Hello${SPACE}  
Should Be Equal ${stripped} Hello  
${stripped}= Strip String ${SPACE}Hello${SPACE} mode=left
Should Be Equal ${stripped} Hello${SPACE}  
${stripped}= Strip String aabaHelloeee characters=abe
Should Be Equal ${stripped} Hello  

New in Robot Framework 3.0.

robotframework的学习笔记(十六)----robotframework标准库String的更多相关文章

  1. python3.4学习笔记(十六) windows下面安装easy_install和pip教程

    python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...

  2. python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

    python3.4学习笔记(十五) 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) python print 不换行(在后面加上,end=''),prin ...

  3. (C/C++学习笔记) 十六. 预处理

    十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...

  4. Python学习笔记011_模块_标准库_第三方库的安装

    容器 -> 数据的封装 函数 -> 语句的封装 类 -> 方法和属性的封装 模块 -> 模块就是程序 , 保存每个.py文件 # 创建了一个hello.py的文件,它的内容如下 ...

  5. JavaScript权威设计--CSS(简要学习笔记十六)

    1.Document的一些特殊属性 document.lastModified document.URL document.title document.referrer document.domai ...

  6. MySQL学习笔记十六:锁机制

    1.数据库锁就是为了保证数据库数据的一致性在一个共享资源被并发访问时使得数据访问顺序化的机制.MySQL数据库的锁机制比较独特,支持不同的存储引擎使用不同的锁机制. 2.MySQL使用了三种类型的锁机 ...

  7. python 学习笔记十六 django深入学习一 路由系统,模板,admin,数据库操作

    django 请求流程图 django 路由系统 在django中我们可以通过定义urls,让不同的url路由到不同的处理函数 from . import views urlpatterns = [ ...

  8. SharpGL学习笔记(十六) 多重纹理映射

    多重纹理就把多张贴图隔和在一起.比如下面示例中,一个表现砖墙的纹理,配合一个表现聚光灯效果的灰度图,就形成了砖墙被一个聚光灯照亮的效果,这便是所谓的光照贴图技术. 多重纹理只在OpenGL扩展库中才提 ...

  9. yii2源码学习笔记(十六)

    Module类的最后代码 /** * Registers sub-modules in the current module. * 注册子模块到当前模块 * Each sub-module shoul ...

  10. Swift学习笔记十六:协议

    Protocol(协议)用于统一方法和属性的名称,而不实现不论什么功能. 协议可以被类.枚举.结构体实现.满足协议要求的类,枚举,结构体被称为协议的遵循者. 遵循者须要提供协议指定的成员,如属性,方法 ...

随机推荐

  1. Java数据结构和算法(七)——链表

    前面博客我们在讲解数组中,知道数组作为数据存储结构有一定的缺陷.在无序数组中,搜索性能差,在有序数组中,插入效率又很低,而且这两种数组的删除效率都很低,并且数组在创建后,其大小是固定了,设置的过大会造 ...

  2. Scala入门系列(九):函数式编程

    引言 Scala是一门既面向对象,又面向过程的语言,Scala的函数式编程,就是Scala面向过程最好的佐证.也真是因此让Scala具备了Java所不具备的更强大的功能和特性. 而之所以Scala一直 ...

  3. 使用FlexPaper实现在线预览

    准备工作 1.下载并安装安装OpenOffice 2.下载并安装FlexPaper 3.下载jodconverter 5.下载并安装swftools 软件安装具体方式按照 这篇博客 :http://b ...

  4. linux crontab yum安装

    crontab工具来做计划任务,定时任务,执行某个脚本等等 1.检查是否已安装crontab # crontab -bash: crontab: command not found 执行 cronta ...

  5. ConcurrentModificationException异常出现的原因

    原因:对list .map 等迭代的时进行修改就会抛出java.util.ConcurrentModificationException异常 比如: public synchronized void ...

  6. logstash 向elasticsearch写入数据,怎样指定多个数据template

    之前在配置从logstash写数据到elasticsearch时,指定单个数据模板没有问题.可是在配置多个数据模板时候,总是不成功,后来找了非常多资料,最终找到解决的方法,就是要多加一个配置项: te ...

  7. 【LDA】修正 GibbsLDA++-0.2 中的两个内存问题

    周末这两天在家用LDA做个小实验. 在LDA的众多实现的工具包中.GibbsLDA 是应用最广泛的.包含c++版本号.java版本号等.GibbsLDA++ 是它的C++版本号的实现.眼下最新版本号是 ...

  8. 【VS2017新特性】在VS中调试javascript脚本

    1   概述 VS2017可以调试JS,本篇文章简要概述VS2017关于启用和关闭VS调试功能. 2   具体内容 当开启VS2017JS调试功能时,我们用VS2017打开解决方案时,会出现如下界面: ...

  9. webpack入门(2)

    webpack入门(2) ps:每个案例都是基于前一个案例改造的 webpack入门(1) 戳这里 案例源码戳这里 十二.ProvidePlugin 自动加载模块 new webpack.Provid ...

  10. MySQL 导入外部数据时报错:1153: Got a packet bigger than 'max_allowed_packet' 解决方案

    MySQL 导入外部数据时报错:1153: Got a packet bigger than 'max_allowed_packet' 解决方案 zoerywzhou@163.com http://w ...