PreviousNextIndex

SUBSTRING statement

Inserts content from a specified expression into a field or variable, optionally replacing existing characters, bytes, or columns.

Syntax

SUBSTRING ( target , position [ , length [ , type ] ] ) = expression 

target
position
length
type
expression
Examples

The r-sub.p procedure uses the SUBSTRING statement to replace a segment of text with the expression in the SUBSTRING statement XXXXXXXXX. The procedure first displays the text you can work with in the Original Text frame. Then the procedure prompts you for the start position of the replacement and the length of the replacement. Under the WORD heading, you see the revised text.

r-sub.p
DEFINE VARIABLE rtext AS CHARACTER NO-UNDO FORMAT "x(50)". 
DEFINE VARIABLE orig  AS CHARACTER NO-UNDO FORMAT "x(31)". 
DEFINE VARIABLE strt  AS INTEGER   NO-UNDO FORMAT ">9". 
DEFINE VARIABLE leng  AS INTEGER   NO-UNDO FORMAT ">9". 
orig = "Now is the time to use OpenEdge". 
DISPLAY orig WITH CENTERED TITLE "Original Text" NO-LABEL. 
REPEAT: 
  rtext = orig. 
  UPDATE strt LABEL "START" leng LABEL "LENGTH". 
  SUBSTRING(rtext, strt, leng, "CHARACTER") = "XXXXXXXXX". 
  DISPLAY rtext LABEL "WORD" WITH CENTERED. 
END. 

The SUBSTRING and OVERLAY statements use the length option differently. For both, the length indicates how much of the target to replace. However, SUBSTRING always inserts the full expression and never pads the expression to match the length. By contrast, the length in OVERLAY determines how much ABL adds to the target, even if ABL must truncate the expression or pad it with spaces. The r-sub-over.p (1 of 2) procedure illustrates the differences between these two statements.

r-sub-over.p
/* This procedure illustrates the differences between the SUBSTRING and 
   OVERLAY statements. */ 
DEFINE VARIABLE cOriginal  AS CHARACTER NO-UNDO INITIAL "OpenEdge". 
DEFINE VARIABLE cSubstring AS CHARACTER NO-UNDO. 
DEFINE VARIABLE cOverlay   AS CHARACTER NO-UNDO. 
DEFINE VARIABLE cResults   AS CHARACTER NO-UNDO. 
/* Default behavior without optional LENGTH. */ 
ASSIGN 
  cSubstring              = cOriginal 
  SUBSTRING(cSubstring,2) = "***" 
  cOverlay                = cOriginal 
  OVERLAY(cOverlay,2)     = "***" 
  cResults                = "target = ~"OpenEdge~". ~n~n"  
    + "If you do not supply a length, SUBSTRING and OVERLAY default as follows: 
    ~n~n" + "SUBSTRING(target,2) = ~"***~"  yields:  " + cSubstring + ". ~n"  
    + "OVERLAY(target,2)     = ~"***~"  yields:  " + cOverlay + ".". 
/* Behavior with zero LENGTH. */ 
ASSIGN 
  cSubstring                = cOriginal 
  SUBSTRING(cSubstring,2,0) = "***" 
  cOverlay                  = cOriginal 
  OVERLAY(cOverlay,2,0)     = "***" 
  cResults                  = cResults + "~n~n"  
    + "For a zero length, SUBSTRING and OVERLAY behave as follows:  ~n~n"  
    + "SUBSTRING(target,2,0) = ~"***~"  yields:  " + cSubstring + ". ~n"  
    + "OVERLAY(target,2,0)     = ~"***~"  yields:  " + cOverlay + ".". 
/* Behavior with LENGTH < replacement. */ 
ASSIGN  
  cSubstring                = cOriginal 
  SUBSTRING(cSubstring,2,1) = "***" 
  cOverlay                  = cOriginal 
  OVERLAY(cOverlay,2,1)     = "***" 
  cResults                  = cResults + "~n~n"  
    + "For a length shorter than the replacement, SUBSTRING and OVERLAY behave 
    as follows: ~n~n" + "SUBSTRING(target,2,1) = ~"***~"  yields:  "  
    + cSubstring + ". ~n" + "OVERLAY(target,2,1)     = ~"***~"  yields:  "  
    + cOverlay + ".". 
/* Behavior with LENGTH = replacement. */ 
ASSIGN  
  cSubstring                = cOriginal 
  SUBSTRING(cSubstring,2,3) = "***" 
  cOverlay                  = cOriginal 
  OVERLAY(cOverlay,2,3)     = "***" 
  cResults                  = cResults + "~n~n"  
    + "For a length equal to the replacement, SUBSTRING and OVERLAY behave as 
    follows:  ~n~n" + "SUBSTRING(target,2,3) = ~"***~"  yields:  "  
    + cSubstring + ". ~n" + "OVERLAY(target,2,3)     = ~"***~"  yields:  "  
    + cOverlay + ".". 
/* Behavior with LENGTH > replacement. */ 
ASSIGN  
  cSubstring                = cOriginal 
  SUBSTRING(cSubstring,2,6) = "***" 
  cOverlay                  = cOriginal 
  OVERLAY(cOverlay,2,6)     = "***" 
  cResults                  = cResults + "~n~n"  
    + "For a length greater than the repalcement, SUBSTRING and OVERLAY behave 
    as follows:  ~n~n" + "SUBSTRING(target,2,6) = ~"***~"  yields:  "  
    + cSubstring + ". ~n" + "OVERLAY(target,2,6)     = ~"***~"  yields:  "  
    + cOverlay + ".". 
MESSAGE cResults VIEW-AS ALERT-BOX. 

Note
See also

OVERLAY statement, SUBSTRING function


OpenEdge Release 10.2B
Copyright © 2009 Progress Software Corporation
PreviousNextIndex