%SCANRPL (扫描和替换字符)
%SCANRPL(scan string : replacement : source { : scan start { : scan length { : *NATURAL | *STDCHARSIZE} } })
%SCANRPL 返回通过将源字符串中出现的所有扫描字符串替换为替换字符串而生成的字符串。 扫描字符串的搜索从扫描开始位置开始,并继续扫描长度。 源字符串中超出扫描开始位置和扫描长度指定的范围的部分将包括在结果中。
第一个,第二个和第三个参数必须为字符,图形或 UCS-2类型。 它们可以是固定长度格式或可变长度格式。
如果第一个或第二个参数的类型或 CCSID 与第三个参数不相同,则转换为第三个参数的类型和 CCSID。
第四个参数表示开始搜索扫描字符串的位置 (以字符计)。 如果未指定,那么起始位置缺省为 1。 该值可以从 1 到源字符串的当前长度。 它可以是任何数字值或没有小数位的数字表达式
第五个参数表示要扫描的源字符串中的字符数。 如果未指定参数,那么长度缺省为从起始位置开始的源字符串的其余部分。 该值必须大于或等于零,并且小于或等于从起始位置开始的源字符串的剩余长度。 它可以是任何数字值或没有小数位的数字表达式
- 指定 *NATURAL 以指示 %SCANRPL 在 CHARCOUNT NATURAL 方式下运行。 起始位置和长度以字符计,而不是以字节或双字节计。 例如,如果源字符串是值为 "ábç12" 的 UTF-8 字符串,那么 3 的起始位置表示 "ç" ,因为它是第三个字符。
- 指定 *STDCHARSIZE 以指示 %SCANRPL 以 CHARCOUNT STDCHARSIZE 方式运行。 在上一个示例中,使用 CHARCOUNT STDCHARSIZE 方式时, 3 的起始位置引用 "b" ,因为它是第三个字节。 字符 "á" 和 "ç" 是 2 字节字符。
返回的值可能大于,等于或小于源字符串。 生成的长度取决于扫描字符串和替换字符串的长度,还取决于执行替换的次数。 例如,假定扫描字符串为 "a" ,替换字符串为 "bc"。 如果源字符串为 "ada" ,那么返回的值的长度为 5 ("bcdbc")。 如果源字符串为 "ddd" ,那么返回的值的长度为 3 ("ddd")。
如果源字符串和替换字符串的长度不同,或者如果任何字符串的长度不同,那么返回的值为可变长度。 否则,返回的值为固定长度。 返回的值与源字符串的类型相同。
仅扫描源字符串中的每个位置一次。 例如,如果扫描字符串为 "aa" ,而源字符串为 "baaaaac" ,那么第一个匹配项位于位置 2 和 3 中。 下一次扫描从位置 4 开始,并在位置 4 和 5 中找到匹配项。 下一次扫描从位置 6 开始,并且找不到任何进一步的匹配项。 如果替换字符串为 "xy" ,那么返回的值为 "bxyxyac"。
提示: 可以使用 %SCANRPL 通过指定空替换字符串从源字符串中完全除去出现的扫描字符串。
// ....+....1....+....2....+....3....+...string1 = 'See NAME. See NAME run. Run NAME run.';
// 1. All occurrences of "NAME" are replaced by the
// replacement value. In the first case,
// the resulting string is shorter than the source
// string, since the replacment string is shorter
// than the scan string. In the second case, the
// resulting string is longer.string2 = %ScanRpl('NAME' : 'Tom' : string1);
// string2 = 'See Tom. See Tom run. Run Tom run.'string2 = %ScanRpl('NAME' : 'Jenny' : string1);
// string2 = 'See Jenny. See Jenny run. Run Jenny run.'
// 2. All occurrences of ** are removed from the string.
// The replacement string, '', has zero length.string3 = '*Hello**There**Everyone*';
string2 = %ScanRpl('**' : '' : string3);
// string2 = '*HelloThereEveryone*'
// 3. All occurrences of "NAME" are replaced by "Tom"
// starting at position 6. Since the first "N" of
// the first "NAME" in the string is not part of the
// source string that is scanned, the first "NAME"
// is not considered replaceable.string2 = %ScanRpl('NAME' : 'Tom' : string1 : 6);
// string2 = 'See NAME. See Tom run. Run Tom run.'
// 4. All occurrences of "NAME" are replaced by "Tom"
// up to length 31. Since the final "E" of
// the last "NAME" in the string is not part of the
// source string that is scanned, , the final "NAME"
// is not considered replaceable.string2 = %ScanRpl('NAME' : 'Tom' : string1 : 1 : 31);
// string2 = 'See Tom. See Tom run. Run NAME run.'
// 5. All occurrences of "NAME" are replaced by "Tom"
// from position 10 for length 10. Only the second
// "NAME" value falls in that range.string2 = %ScanRpl('NAME' : 'Tom' : string1 : 10 : 10);
// string2 = 'See NAME. See Tom run. Run NAME run.'