Stacked
Stacked

Reputation: 263

Progress 4gl format string with constants

Is there a way to format a string in Progress with a specific character?

An example is showing an SSN with x's for the first 6 numbers. I've attempted to use the String function, but it does not respect the letter x being sent in with the format.

SSNString = '333224444'.
SSNString = String(SSNString, "xxx-xx-9999").
//This displays 333-22-4444 instead of xxx-xx-4444.

Upvotes: 3

Views: 4283

Answers (2)

Bruno
Bruno

Reputation: 102

The STRING function parameter is intended only to format the output string to a desired pattern, not to replace chars on it "on the fly". You'll need a CHAR variable to put the database value on it and you can use the OVERLAY or SUBSTRING function to override the first five chars on your string to match your criteria. In your example :

SSNString = '333224444'.

/* Choose the OVERLAY function or SUBSTRING function. You can pass either
   a FILL function call, one variable or a fixed string value to OVERLAY 
   or SUBSTRING function call. They'll behave exactly the same way. */

OVERLAY(SSNString,1,5,'CHAR') = FILL('X',5).
SUBSTRING(SSNString,1,5) = 'XXXXX'.

/* At this point, SSNString is XXXXX4444 */

SSNString = STRING(SSNString,'XXX-XX-9999').

/* Here you'll have XXX-XX-4444 */

MESSAGE SSNString
    VIEW-AS ALERT-BOX INFO BUTTONS OK.

In a little more complex and flexible way you can use a function that returns the value formatted as you desire and dismiss the need to use a variable in cases which you want to apply special format to database field values.

FUNCTION formatSSNString RETURNS CHAR
    ( INPUT pSourceString   AS CHAR,
      INPUT pFormat         AS CHAR,
      INPUT pOverlayCount   AS INT,
      INPUT pStart          AS INT,
      INPUT pCharOverlay    AS CHAR ):

    DEF VAR cCharOutput     AS CHAR NO-UNDO.

    cCharOutput = pSourceString.
    OVERLAY(cCharOutput,pStart,pOverlayCount,'CHAR') = FILL(pCharOverlay,pOverlayCount).
    cCharOutput = STRING(cCharOutput,pFormat).

    RETURN cCharOutput.

END FUNCTION.

DEF VAR SSNString       AS CHAR NO-UNDO.

SSNString = '333224444'.

/* This returns XXX-XX-4444 */
MESSAGE formatSSNString(SSNString, 'xxx-xx-9999', 5, 1, 'X') 
    VIEW-AS ALERT-BOX.

/* This returns 33X-XX-X444 */
MESSAGE formatSSNString(SSNString, 'xxx-xx-9999', 4, 3, 'X') 
    VIEW-AS ALERT-BOX.

/* This returns 333-XX-4444 */
MESSAGE formatSSNString(SSNString, 'xxx-xx-9999', 2, 4, 'X') 
    VIEW-AS ALERT-BOX.

/* This returns 333-XX-44YY */
MESSAGE formatSSNString(formatSSNString(SSNString, 'x(9)', 2, 4, 'X'), 'xxx-xx-9999', 2, 8, 'Y') 
    VIEW-AS ALERT-BOX.

Hope it helps.

Upvotes: 1

doydoy44
doydoy44

Reputation: 5772

I don't know a way to change this format with String function.

If you want to keep only the last four digits, you can try something like this:

SSNString = '333224444'.
SSNString = "xxx-xx-" + SUBSTRING(SSNString, 6, 4).

Upvotes: 1

Related Questions