StringUtils

Robot Framework library documentation

StringUtils Documentation

Version 2.1.0 Scope GLOBAL 21 Keywords
Author: Bruce WayneMaintainer: Docs TeamLicense: MITRobot Framework: >=7.0Python: >=3.11
Sample Usage
Robot
*** Settings ***
Library    StringUtils

*** Test Cases ***
Example
    [Documentation]    Demonstrates using StringUtils
    # add your keyword calls here

Introduction

String utilities library for Robot Framework.

This library provides comprehensive string operations including:

  • String manipulation and transformation
  • Pattern matching and validation
  • Encoding and hashing
  • Text formatting and templating
  • Data extraction and parsing

Arguments

text : str

Return Type

str

Documentation

Capitalize the first letter of each word in a string.

Arguments:

  • text: Input string to capitalize

Returns: String with each word capitalized

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Capitalize Text Example
    ${result}    Capitalize Words    hello world
    Should Be Equal     ${result}    Hello World

Arguments

text : str
mode : str default: "all"

Return Type

str

Documentation

Remove whitespace from a string.

Arguments:

  • text: Input string
  • mode: Removal mode - "all" (remove all), "leading" (remove leading),

"trailing" (remove trailing), "both" (remove both ends)

Returns: String with whitespace removed

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Remove Whitespace Example
    ${result}    Remove Whitespace    "  hello world  "    mode=both
    Should Be Equal     ${result}    hello world

Arguments

text : str

Return Type

List[int]

Documentation

Extract all numbers from a string.

Arguments:

  • text: Input string containing numbers

Returns: List of extracted integers

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Extract Numbers Example
    ${numbers}    Extract Numbers    Price: $99.99, Quantity: 5
    Should Contain     ${numbers}    ${99}
    Should Contain     ${numbers}    ${5}

Arguments

text : str

Return Type

List[str]

Documentation

Extract all email addresses from a string.

Arguments:

  • text: Input string containing email addresses

Returns: List of extracted email addresses

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Extract Emails Example
    ${emails}    Extract Emails    Contact: john@example.com or jane@test.org
    Should Contain     ${emails}    john@example.com
    Should Contain     ${emails}    jane@test.org

Arguments

url : str

Return Type

bool

Documentation

Validate if a string is a valid URL.

Arguments:

  • url: URL string to validate

Returns: True if valid URL, False otherwise

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Validate URL Example
    ${valid}    Validate Url    https://example.com
    Should Be True     ${valid}

Arguments

text : str
visible_chars : int default: 4

Return Type

str

Documentation

Mask sensitive data, showing only first few characters.

Arguments:

  • text: Text to mask
  • visible_chars: Number of characters to keep visible (default: 4)

Returns: Masked string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Mask Sensitive Data Example
    ${masked}    Mask Sensitive Data    secret12345    visible_chars=3
    Should Start With     ${masked}    sec
    Should Contain     ${masked}    *

Arguments

text : str
algorithm : str default: "sha256"

Return Type

str

Documentation

Generate hash of a string.

Arguments:

  • text: Text to hash
  • algorithm: Hash algorithm - "md5", "sha1", "sha256" (default)

Returns: Hexadecimal hash string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Hash String Example
    ${hash}    Hash String    password123    algorithm=sha256
    Should Not Be Empty     ${hash}
    Length Should Be     ${hash}    ${64}

Arguments

text : str

Return Type

str

Documentation

Encode string to Base64.

Arguments:

  • text: Text to encode

Returns: Base64 encoded string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Encode Base64 Example
    ${encoded}    Encode Base64    Hello World
    Should Not Be Empty     ${encoded}

Arguments

encoded_text : str

Return Type

str

Documentation

Decode Base64 string.

Arguments:

  • encoded_text: Base64 encoded string

Returns: Decoded string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Decode Base64 Example
    ${decoded}    Decode Base64    SGVsbG8gV29ybGQ=
    Should Be Equal     ${decoded}    Hello World

Arguments

text : str
pattern : str
replacement : str
case_sensitive : bool default: True

Return Type

str

Documentation

Replace text matching a regex pattern.

Arguments:

  • text: Input text
  • pattern: Regular expression pattern
  • replacement: Replacement string
  • case_sensitive: Whether pattern matching is case sensitive (default: True)

Returns: Text with replacements applied

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Replace Pattern Example
    ${result}    Replace Pattern    Hello 123 World    \d+    NUMBER
    Should Be Equal     ${result}    Hello NUMBER World

Arguments

text : str
separator : str default: None
max_split : int default: ...

Return Type

List[str]

Documentation

Split string into a list.

Arguments:

  • text: Text to split
  • separator: Separator string (default: whitespace)
  • max_split: Maximum number of splits (default: -1 for unlimited)

Returns: List of strings

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Split String Example
    ${parts}    Split String    apple,banana,cherry    separator=,
    Should Contain     ${parts}    apple
    Should Contain     ${parts}    banana
    Should Contain     ${parts}    cherry

Arguments

strings : List[str]
separator : str default: " "

Return Type

str

Documentation

Join list of strings with a separator.

Arguments:

  • strings: List of strings to join
  • separator: Separator string (default: space)

Returns: Joined string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Join Strings Example
    @{items}    Create List    apple    banana    cherry
    ${result}    Join Strings    ${items}    separator=,
    Should Be Equal     ${result}    apple,banana,cherry

Arguments

template : str

Return Type

str

Documentation

Format a string template with variables.

Arguments:

  • template: Template string with {variable} placeholders
  • kwargs: Variable name-value pairs

Returns: Formatted string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Format Template Example
    ${result}    Format Template    Hello {name}, you are {age} years old
    ...    name=John    age=30
    Should Be Equal     ${result}    Hello John, you are 30 years old

Arguments

text : str
substring : str
case_sensitive : bool default: True

Return Type

int

Documentation

Count occurrences of a substring in text.

Arguments:

  • text: Text to search in
  • substring: Substring to count
  • case_sensitive: Whether search is case sensitive (default: True)

Returns: Number of occurrences

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Count Occurrences Example
    ${count}    Count Occurrences    hello hello world    hello
    Should Be Equal As Integers     ${count}    ${2}

Arguments

text : str
max_length : int
suffix : str default: "..."

Return Type

str

Documentation

Truncate string to maximum length.

Arguments:

  • text: Text to truncate
  • max_length: Maximum length
  • suffix: Suffix to append if truncated (default: "...")

Returns: Truncated string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Truncate String Example
    ${result}    Truncate String    This is a very long text    10
    Should Be Equal     ${result}    This is a...

Arguments

text : str
width : int
padding : str default: " "
align : str default: "left"

Return Type

str

Documentation

Pad string to specified width.

Arguments:

  • text: Text to pad
  • width: Target width
  • padding: Padding character (default: space)
  • align: Alignment - "left", "right", or "center" (default: left)

Returns: Padded string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Pad String Example
    ${result}    Pad String    hello    10    align=right
    Should Be Equal     ${result}    ${SPACE * 5}hello

Arguments

text : str

Return Type

str

Documentation

Reverse a string.

Arguments:

  • text: Text to reverse

Returns: Reversed string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Reverse String Example
    ${result}    Reverse String    hello
    Should Be Equal     ${result}    olleh

Arguments

text : str
separator : str default: " "

Return Type

str

Documentation

Remove duplicate words/parts from a string.

Arguments:

  • text: Text to process
  • separator: Separator to split by (default: space)

Returns: String with duplicates removed

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Remove Duplicates Example
    ${result}    Remove Duplicates    apple banana apple cherry
    Should Be Equal     ${result}    apple banana cherry

Arguments

phone : str
country : str default: "US"

Return Type

bool

Documentation

Validate phone number format.

Arguments:

  • phone: Phone number string
  • country: Country code for validation (default: US)

Returns: True if valid format, False otherwise

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Validate Phone Number Example
    ${valid}    Validate Phone Number    +1-555-123-4567
    Should Be True     ${valid}

Arguments

text : str

Return Type

Optional[Dict[str, Any]]

Documentation

Extract and parse JSON object from text.

Arguments:

  • text: Text containing JSON

Returns: Parsed JSON dictionary or None if not found

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Extract JSON From Text Example
    ${json}    Extract Json From Text    Data: {"name": "John", "age": 30}
    Should Be Equal     ${json}[name]    John
    Should Be Equal As Integers     ${json}[age]    ${30}

Arguments

length : int default: 10
include_uppercase : bool default: True
include_lowercase : bool default: True
include_digits : bool default: True
include_special : bool default: False

Return Type

str

Documentation

Generate a random string.

Arguments:

  • length: Length of string (default: 10)
  • include_uppercase: Include uppercase letters (default: True)
  • include_lowercase: Include lowercase letters (default: True)
  • include_digits: Include digits (default: True)
  • include_special: Include special characters (default: False)

Returns: Random string

Example:

*** Settings ***
Library    StringUtils


*** Test Cases ***
Generate Random String Example
    ${random}    Generate Random String    length=16    include_special=True
    Length Should Be     ${random}    ${16}
    Should Match Regexp     ${random}    .+

Need Help?

Found an issue or have a feature request for this library? Let the maintainers know by opening a new GitHub issue. Please include environment details and relevant log output to help us reproduce the problem.

Open an Issue on GitHub