字串操作

有一些有用的內建函式可以處理字串。使用字元位置的所有函式都以索引 1 作為第一個字元開始,而不是在許多語言中常見的 0。

STRING - 將任何值轉換為字串

此示例將整數 2000 轉換為字串 2000

DEFINE VARIABLE i AS INTEGER     NO-UNDO.
DEFINE VARIABLE c AS CHARACTER   NO-UNDO.

i = 2000.

c = STRING(i).

DISPLAY c.

CHRASC - 將單個字元轉換為 ascii 和從 ascii 轉換。

CHR(整數)

返回 ascii 程式碼整數的字元表示形式

ASC(字)

返回字元的 ascii 整數值

DEFINE VARIABLE ix     AS INTEGER   NO-UNDO.
DEFINE VARIABLE letter AS CHARACTER NO-UNDO FORMAT "X(1)" EXTENT 26.

DO ix = 1 TO 26:
  letter[ix] = CHR((ASC("A")) - 1 + ix).
END.

DISPLAY SKIP(1) letter WITH 2 COLUMNS NO-LABELS
  TITLE "T H E  A L P H A B E T".

LENGTH - 返回字串的長度

長度(字串)。 //返回一個長度為字串的整數。

DEFINE VARIABLE cString AS CHARACTER   NO-UNDO.

cString = "HELLO".

MESSAGE "The string " cString " is " LENGTH(cString) " characters long" VIEW-AS ALERT-BOX. 

SUBSTRING - 返回或指定字串的一部分

  • SUBSTRING(字串,起始位置,長度)。

從位置起始位置開始,從字串返回長度字元。

  • SUBSTRING(字串,起始位置)。

返回 string 的其餘部分,從起始位置開始

DEFINE VARIABLE cString AS CHARACTER   NO-UNDO.

cString = "ABCDEFGH".

DISPLAY SUBSTRING(cString, 4, 2). //Displays "DE" 
DISPLAY SUBSTRING(cString, 4). //Displays "DEFGH"

子字串也可用於覆蓋字串的一部分。使用相同的語法,但改為指定子字串:

DEFINE VARIABLE cString AS CHARACTER   NO-UNDO.

cString = "ABCDEFGH".
 
SUBSTRING(cString, 4, 2) = "XY". //Replaces position 4 and 5 with "XY" 

DISPLAY cString.  

還有一個名為 OVERLAY 的類似函式,這個例子來自 Progress 文件,涵蓋了 OVERLAYSUBSTRING 之間的區別:

/* 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 replacement, 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.

INDEX - 返回字串中字串的位置。

R-INDEX 會做同樣的事情,但從右到左搜尋。

INDEX(來源,目標)

在源內搜尋目標(從左到右)並返回它的位置。如果缺少則返回 0。

INDEX(來源,目標,起始位置)。

與上述相同,但開始在起始位置搜尋

DEFINE VARIABLE str AS CHARACTER   NO-UNDO.

str = "ABCDEFGH".

DISPLAY INDEX(str, "cd") INDEX(str, "cd", 4). //Will display 3 and 0

REPLACE - 替換字串中的字串。

REPLACE(string,from-string,to-string)

用字串替換 from-string 和 to-string。從字串到字串不需要具有相同的長度,to-string 也可以不是(“”)來刪除字元。

DEFINE VARIABLE c AS CHARACTER   NO-UNDO.

c = "ELLO".

DISPLAY REPLACE(c, "E", "HE"). // Displays "HELLO"

c = "ABABABA".

DISPLAY REPLACE(c, "B", ""). // Remove all Bs

TRIM - 刪除前導和尾隨空格(或其他字元)。

這在清理 indata 時非常有用。

TRIM(字串)

刪除所有前導和尾隨空格,製表符,換行符,回車符。

TRIM(字串,字元)。

刪除所有前導和尾隨字元

LEFT-TRIMRIGHT-TRIM 做同樣的事情但只是領先或尾隨。

DEFINE VARIABLE c AS CHARACTER   NO-UNDO.

c = "__HELLO_WORLD_____".

DISPLAY TRIM(c, "_").
/*Displays HELLO_WORLD without all the leading and 
trailing underscores but leaves the one in the middle.
REPLACE would have removed that one as well */

SUBSTITUTE - 用字串替換引數。

SUBSTITUTE is a limited function for replacing up to nine preformatted parameters in a string.

SUBSTITUTE(string,param1,param2,…,param9)。

引數必須採用 &1&9 的格式。

如果你想在字串中使用&符號(而不是使用它作為引數),請使用另一個&符號:&&

DEFINE VARIABLE str AS CHARACTER   NO-UNDO.
                                      
str = "&1 made &2 goals in &3 games playing for &4".

MESSAGE SUBSTITUTE(str, "Zlatan Ibrahimovic", 113, 122, "Paris Saint-Germain") VIEW-AS ALERT-BOX.
MESSAGE SUBSTITUTE(str, "Mats Sundin", 555, 1305, "Toronto Maple Leafs") VIEW-AS ALERT-BOX.

引數可以在字串中出現多次,所有引數都將被替換:

MESSAGE SUBSTITUTE("&1 &2 or not &1 &2", "To", "Be") VIEW-AS ALERT-BOX.