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'
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() .
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.
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).
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'
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
".
In Variable
part, I used the most simple interpolation
${a} Hello
${b} World
${c} ${a}${b}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With