Search code examples
informix4gl

String alignment with spaces in 4GL


How to right align a string in 4gl with specified no.of spaces?


Solution

  • You can use the FILL function to create a specified number of spaces. For example, FILL("A", 10) will return "AAAAAAAAAA".

    Here's is a simple demo:

    DEFINE VARIABLE cText   AS CHARACTER   NO-UNDO.
    DEFINE VARIABLE iLength AS INTEGER     NO-UNDO.
    
    iLength = 16.
    cText = "Some text".
    
    cText = FILL(" ", iLength - LENGTH(cText)) + cText.
    
    MESSAGE cText.
    

    Your required overall length is 16 characters. Use LENGTH(cText) to determine how many characters your text takes up, and then use FILL to create the remaining number of spaces at the front of the string.


    Informix

    I answered this question for Progress 4GL, and only noticed afterwards that the question was asked about Informix 4GL. I am very sorry for my mistake.

    You can use the same principal to do this in Informix. You use the LENGTH keyword to find out how long your existing text is, but instead of using FILL() to pad it with spaces, you should use SPACES.

    Progress: FILL(" ", 10) + c_text
    Informix: 10 SPACES, c_text
    

    I really hope this helps you.