DEV Community

Peter + AI
Peter + AI

Posted on

πŸ”€ String Interpolation in Uniface 10.4: A Practical Guide

Note: This article was created with the assistance of AI. πŸ€–

πŸ“Œ Introduction

If you're coming from modern languages like C# or JavaScript, you might expect string interpolation to work with template literals or dollar signs. In Uniface 10.4, things work a bit differently! Let's explore how to combine strings and variables in Uniface. πŸ’ͺ

πŸš€ The %% Operator: Your Best Friend

The main way to combine strings in Uniface is using the %% operator. This is Uniface's version of string concatenation.

Basic Example:

lv_username = "John"
lv_message = "Hello %%lv_username%%, welcome back!"
; Result: "Hello John, welcome back!"
Enter fullscreen mode Exit fullscreen mode

You can chain multiple variables together:

lv_firstname = "Jane"
lv_lastname = "Smith"
lv_fullname = "%%lv_firstname%% %%lv_lastname%%"
; Result: "Jane Smith"
Enter fullscreen mode Exit fullscreen mode

πŸ”§ The $concat Function

Uniface also provides the $concat function, which can combine up to 5 strings at once. This is useful when you need to merge multiple pieces of text. ✨

Syntax:

$concat(String1, String2, String3, String4, String5)
Enter fullscreen mode Exit fullscreen mode

Example:

lv_name = "Alice"
lv_status = "Active"
lv_result = $concat("User: ", lv_name, " - Status: ", lv_status)
; Result: "User: Alice - Status: Active"
Enter fullscreen mode Exit fullscreen mode

βœ‚οΈ Extracting Parts of Strings

Sometimes you need to grab specific characters from a string. Uniface uses square brackets for this:

lv_name = "Alexander"
lv_short = lv_name[1:4]
; Result: "Alex"

lv_rest = lv_name[5]
; Result: "ander"
Enter fullscreen mode Exit fullscreen mode

⚠️ Common Pitfalls

1. No Template Literals

Unlike modern languages, Uniface doesn't have syntax like $"Hello {name}". You must use the %% operator or $concat explicitly.

2. The 5-Parameter Limit

The $concat function only accepts 5 parameters maximum. If you need more, nest multiple calls:

lv_result = $concat($concat(string1, string2, string3, string4, string5), string6)
Enter fullscreen mode Exit fullscreen mode

3. Performance in Loops

Repeated string concatenation inside loops can slow down your application. Consider building strings step by step with intermediate variables. 🐌

πŸ’‘ Pro Tips

Tip 1: Use the %% operator for simple concatenations - it's more readable! πŸ‘€

Tip 2: Use $concat when combining multiple fixed strings and variables in one expression. 🎯

Tip 3: Create helper functions for complex string operations, like replacing placeholders in templates. πŸ› οΈ

🎯 Real-World Example

Let's build a user notification message:

lv_user = "Bob"
lv_count = "5"
lv_item = "messages"

lv_notification = $concat("Hi ", lv_user, "! You have ", lv_count, " new ", lv_item, ".")
; Result: "Hi Bob! You have 5 new messages."
Enter fullscreen mode Exit fullscreen mode

🏁 Conclusion

String interpolation in Uniface 10.4 might feel old-school compared to modern languages, but it's straightforward once you understand the %% operator and $concat function. These tools give you everything you need to build dynamic strings in your Uniface applications. πŸš€

Happy coding! πŸ’»βœ¨

Top comments (0)