سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

When using DISPLAY you can specify its location:

DISPLAY l_string at 3,3

Similarly, you can set a variable as a placeholder for the location:

DEFINE l_dest1, l_dest2 int
DEFINE l_string char(20)

LET l_string = "Some text here"
LET l_dest1 = 3
LET l_dest2 = 6
DISPLAY l_string at l_dest1, l_dest2 

Result:

                                                                           Some text here

You can also just add spaces before the string like:

DISPLAY"                                MY TEXT"

If the text is in a variable and you don't want to add spaces to that variable. You can do like this:

DISPLAY "                                         ", l_string

See also my answer here on how to center a string. I hope that helps.

You use the USING statement in Informix 4GL. This can be used to do all sorts of formatting, including padding.

LET l_string = "X" 
LET l_string = l_string USING "######"

This will change a string "X" to have 5 spaces in front.

Also useful for padding numbers with zero. USING "&&&&&&" changes 1 to 00001.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top