This blog post was created with AI assistance to help fellow developers understand Uniface functions better. π€
π What is $displaylength?
The $displaylength function in Uniface 10.4 is a useful ProcScript function that tells you how many bytes a string takes up when displayed on screen. Think of it as measuring the "visual size" of your text in the system character set.
π Basic Syntax
Using this function is straightforward:
$displaylength(String)
The function accepts:
- String parameter: Any string value, field reference, variable, or function that returns a string
- Return value: Number of bytes (as a numeric value)
π‘ Practical Examples
Here are some real-world scenarios where $displaylength comes in handy:
Example 1: Simple String Measurement
variables
string vMyText
numeric vLength
end
vMyText = "Hello World"
vLength = $displaylength(vMyText)
; vLength will be 11 (bytes)
Example 2: Field Validation
; Check if a field's display length exceeds limit
if ($displaylength(CUSTOMER.NAME) > 50)
putmess "Customer name too long for display"
endif
Example 3: Unicode Text Handling
; Useful for international characters
vUnicodeText = "MΓΌller & AssociΓ©s"
vDisplayBytes = $displaylength(vUnicodeText)
; Accounts for special characters properly
β οΈ Important Things to Remember
- System Character Set: The function measures based on your system's character encoding π€
- Bytes, not Characters: Returns byte count, which may differ from character count with Unicode
- Universal Use: Works in all Uniface component types (forms, services, reports) β
- Display-Focused: Specifically for display purposes, not storage calculations
π― When to Use $displaylength
This function is particularly useful for:
- π₯οΈ UI Layout: Ensuring text fits properly in display areas
- π Report Formatting: Calculating column widths dynamically
- π Internationalization: Handling different character sets correctly
- β Data Validation: Checking display constraints before showing data
π Related Functions
You might also want to explore $SYS_CHARSET to understand your system's character encoding better. The deprecated displaylength function (without the $) should be avoided in favor of this newer version.
π Conclusion
The $displaylength function is a simple but powerful tool for Uniface developers. Whether you're building international applications or just need to ensure proper text display, this function provides accurate byte measurements for your strings. Remember to always consider the system character set when working with display calculations! π
Happy coding with Uniface! π»
Top comments (0)