字符串操作

有一些有用的内置函数可以处理字符串。使用字符位置的所有函数都以索引 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.