cancel
Showing results for 
Search instead for 
Did you mean: 

Elegant way for String interpolation

Henchway
Contributor

Hello,
I’m wondering if there is any elegant way in the Snaplogic Expression Language to achieve String interpolation? E.g. similar to f-strings in Python?

f"This is a test string including a {variable}"

The method I’m using at the moment is string concatenation, e.g.:

"SELECT " + [lib.env.salesforceGPDeliveryNumberField,lib.env.salesforceGPOperationsNumberField,"Id"].toString() + " FROM Opportunity WHERE " + lib.env.salesforceOpportunityIdField + " = '" +  _opportunityId + "'"

This becomes fairly tedious with longer queries and prone to overlooking the one or other space or quote.
Concat might be a bit better than that, but still is somewhat tedious in comparison to string interpolation.
Is there a feature that I’ve overlooked or is there just no user-friendly method available?

Best regards
Thomas

8 REPLIES 8

j_angelevski
Contributor III

Hi @Henchway,

You can also use the built-in function .sprintf().
For example, you could use the following expression:

"SELECT %s,%s,Id FROM Opportunity WHERE %s =\'%s\'".sprintf("one", "two", "someId", "123")

Which translates to:

SELECT one,two,Id FROM Opportunity WHERE someId ='123'

Henchway
Contributor

Thank you both, I’ll give both solutions a try once i’m back in the office.

alex_panganiban
Contributor

Would using eval() work for you? There’s still some concatenation involved, but I’m not really sure how you could get away from it entirely.

image

koryknick
Employee
Employee

I think that @j.angelevski has a great solution for you with using the sprintf() String method.

Alternatively, if you are querying from a database using an Execute snap (e.g. SQL Server - Execute), you can use variable names without enabling the expression evaluation in the SQL Statement expression box. For example, here is my input document to a SQL Server Execute snap:
image
And the SQL Server Execute settings:
image
Produce this result:
image

Note that the database snaps are some of the only snaps that do this (to my knowledge).