DEV Community

Peter + AI
Peter + AI

Posted on

πŸ“ Understanding Uniface $displaylength Function: A Developer's Guide

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Example 3: Unicode Text Handling

; Useful for international characters
vUnicodeText = "MΓΌller & AssociΓ©s"
vDisplayBytes = $displaylength(vUnicodeText)
; Accounts for special characters properly
Enter fullscreen mode Exit fullscreen mode

⚠️ 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)