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!"
You can chain multiple variables together:
lv_firstname = "Jane"
lv_lastname = "Smith"
lv_fullname = "%%lv_firstname%% %%lv_lastname%%"
; Result: "Jane Smith"
π§ 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)
Example:
lv_name = "Alice"
lv_status = "Active"
lv_result = $concat("User: ", lv_name, " - Status: ", lv_status)
; Result: "User: Alice - Status: Active"
βοΈ 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"
β οΈ 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)
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."
π 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)