Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to concatenate two strings in robot framework .?

Given two strings 'a' , 'b', what is the simplest way to concatenate them and assign to a new variable in robot framework.?

I tried this simple pythonic way, but it didn't work

${var}= 'a' + 'b'
like image 567
user3170122 Avatar asked Oct 03 '17 07:10

user3170122


People also ask

How do you join two strings in Robot Framework?

If you need to assign the result to a variable that you use only once, you could instead do an inline expression e.g. using Python's str. join() , directly where you would use that variable. Catenate ultimately uses str. join() .

Which is the correct way to concatenate 2 strings?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

What operator can be used to combine 2 strings?

Using the + operator is the most common way to concatenate two strings in Java. You can provide either a variable, a number, or a String literal (which is always surrounded by double quotes).


3 Answers

You can use Catenate from BuiltIn.

Example from docs:

${str1} =   Catenate    Hello   world   
${str2} =   Catenate    SEPARATOR=---   Hello   world
${str3} =   Catenate    SEPARATOR=  Hello   world
=>
${str1} = 'Hello world'
${str2} = 'Hello---world'
${str3} = 'Helloworld'
like image 176
Oleh Rybalchenko Avatar answered Oct 21 '22 12:10

Oleh Rybalchenko


Catenate is the usual way to go with strings, as pointed in the other answer.
Alternative option is to use just Set Variable:

${a}=    Set Variable   First
${b}=    Set Variable   Second

${c}=    Set Variable   ${a}${b}
Log To Console    ${c}    # prints FirstSecond

${c}=    Set Variable   ${a} ${b}
Log To Console    ${c}    # prints First Second

${c}=    Set Variable   ${a}-/-${b}
Log To Console    ${c}    # prints First-/-Second

The explaination is that the RF processing of any keyword's arguments - Set Variable including, goes through substituting any variable with its value. E.g. for this call:

Set Variable   ${a}-/-${b}

What roughly happens is "the end value is the value of variable a-/-the value of variable b".

like image 33
Todor Minakov Avatar answered Oct 21 '22 14:10

Todor Minakov


In Variable part, I used the most simple interpolation

${a}   Hello
${b}   World
${c}   ${a}${b}
like image 43
WesternGun Avatar answered Oct 21 '22 12:10

WesternGun