%CHECKR(反向检查)
%CHECKR(comparator : base {: start {: *NATURAL | *STDCHARSIZE}})
%CHECKR 返回字符串 base 的最后一个位置,该字符串包含未在字符串 比较器中出现的字符。 如果 base 中的所有字符也出现在 比较器中,那么该函数将返回 0。
检查从起始位置开始,向左继续,直到找到比较器字符串中未包含的字符为止。 起始位置缺省为字符串的末尾。
第一和第二个参数必须是字符、图形或 UCS-2 类型,长度固定或可变。 如果第一个参数的类型和 CCSID 与第二个参数不同,则第一个参数将转换为第二个参数的类型和 CCSID。
如果指定了第三个参数,那么该参数必须是非浮点数字,且不带小数位。
第三个或第四个参数可以是 *NATURAL 或 *STDCHARSIZE ,以覆盖语句的当前 CHARCOUNT 方式。 如果指定了此参数,那么它必须是最后一个参数。
- 指定 *NATURAL 以指示 %CHECKR 以 CHARCOUNT NATURAL 方式运行。 开始位置和返回值以字符计,而不是以字节或双字节计。 例如,如果基本字符串是值为 "á x ç" 的 UTF-8 字符串,而比较器字符串是 "ç b á" ,那么 "x" 字符是在基本字符串中未出现在比较器字符串中的第一个字符。 使用 CHARCOUNT NATURAL 方式时, %CHECKR 返回 2 ,因为字符 "x" 是基本字符串中的第二个字符。
- 指定 *STDCHARSIZE 以指示 %CHECKR 以 CHARCOUNT STDCHARSIZE 方式运行。 在上一个示例中,使用 CHARCOUNT STDCHARSIZE 方式时, %CHECKR 将返回 3 ,因为 "x" 是字符串中的第三个字节。 字符 "á" 和 "ç" 是 2 字节字符。
注: 由于 /CHARCOUNT 编译器伪指令或 CHARCOUNT 控制关键字, %CHECKR 也可以在 CHARCOUNT NATURAL 方式下运行。
*..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
*---------------------------------------------
* If a string is padded at the end with some
* character other than blanks, the characters
* cannot be removed using %TRIM.
* %CHECKR can be used for this by searching
* for the last character in the string that
* is not in the list of "pad characters".
*---------------------------------------------
D string1 s 50a varying
D inz('My *dog* Spot.* @ * @ *')
D string2 s 50a varying
D inz('someone@somewhere.com')
D padChars C ' *@'
/free
%len(string1) = %checkr(padChars:string1);
// %len(string1) is set to 14 (the position of the last character
// that is not in "padChars").
// string1 = 'My *dog* Spot.'
%len(string2) = %checkr(padChars:string2);
// %len(string2) is set to 21 (the position of the last character
// that is not in "padChars").
// string2 = 'someone@somewhere.com' (the string is not changed)
/end-free *..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
*------------------------------------------------------
* A string contains a numeric value, but it might
* be surrounded by blanks and asterisks and might be
* preceded by a currency symbol.
*------------------------------------------------------
D string s 50a varying inz('$****12.345*** ')
/free
// Find the position of the first character that is not one of ' $*'
numStart = %CHECK (' $*' : string);
// = 6
// Find the position of the last character that is not one of ' *'
numEnd = %CHECKR (' *' : string);
// = 11
// Extract the numeric string
string = %SUBST(string : numStart : numEnd - numStart + 1);
// = '12.345'
/end-free