In RPG IV how can I take a string and eliminate all instances of a character in specific or replace them with another one ?. This is kind of like string replace built in methods in other programmnig languages. Ex: take 021-123450-23-4 and covert to 021123450234
5 回答
%xlate 的正确语法是:
%XLATE(从:to:string{:startpos})
用 & 号替换所有连字符:
new_string = %xlate('-':'&':string);
要删除所有连字符:
您不能使用 &xlate 删除字符。7.1 为我们提供了 %scanrpl,但在此之前,请使用如下内容:
for i = 1 to %len(string);
char = %subst(string:i:1);
if char <> '-';
new_string += char;
endif;
endfor;
看看以下文章:
- http://www.itjungle.com/fhg/fhg030409-story01.html
- http://www.itjungle.com/fhg/fhg022509-story01.html
这些应该会有所帮助。
我有同样的问题。所以我编写了自己的 RPG 程序来为我做这件事:
**
**
D************************************************************************
D* *
D* Procedure 'skReplace' -- Replaces text in 'text' string, *
D* searching for 'find' string, *
D* replacing with 'new' string. *
D* All occurances are replaced, not just one. *
D* Parameters: @txt = 'text' string *
D* @fnd = 'find' string *
D* @new = 'new' string (that replaces 'find' in 'source') *
D* *
D* Update history: *
D* 2013-04 Created by Shawn Kovac. *
D* *
D************************************************************************
D*
P skReplace B
D skReplace PI 999A Varying
D @txt 999A VALUE Varying
D @fnd 999A VALUE Varying
D @new 999A VALUE Varying
D @pos S 3 0
D*
/free
if (%Len(@fnd) = 0); // text to find cannot be empty.
return @txt;
endif;
@pos = 1;
dou (@pos = 0);
@pos = %scan(@fnd: @txt: @pos);
if (@pos > 0);
@txt = %replace( @new : @txt : @pos : %Len(@fnd) );
@pos = @pos + %Len(@new);
if (@pos > %Len(@txt));
@pos = 0;
endif;
endif;
enddo;
return @txt;
/end-free
P skReplace E
**
**
由于 RPG 对内容在哪一列非常挑剔,因此在复制和重用此代码时,您可能需要调整粘贴的文本,因此在 'D*'、'**' 和 'P skReplace 之前有 5 个空格。 ...'。'/free' 前有六个空格。'/free' 行之间的所有代码都有 7 个或更多空格。
我欢迎任何有关改进此代码的建议。如果有人想要的话,我也有 Left、Right 和 Mid 的程序。如果你这样做,就给我发信息。我很乐意分享它们。我知道 RPG 有一个 '%subst' 函数,但许多编程语言都很挑剔,如果参数无效,它们会出错。我的反而提供了更多的灵活性,例如,Left('aoeu', -1) 返回 'aoe' (完整字符串短 1 个字符),而 Right('aoeu', -1) 返回 'oeu' (右侧部分删除 1 个字符后的字符串)。我的 Mid 过程还允许负开始位置,也从字符串的末尾开始索引,我发现这对我很有用。但对于任何想花时间向我要的人来说,它们都是免费的。
快乐编码!
要删除一个字符,您可以使用它
strRes = %scanrpl('-':'':strSrc);
如果您的字符串始终具有该格式(3个字母)-(6个字母)-(2个字母)-(1个字母),您可以使用数据结构:DS 15 Mystuc 1 3 part1 5 10 part2 12 13 part3 15 15 part4
然后将 mystruct 的每个部分移动到另一个结构以获取完整的数字或不带 -movel Michar mystruc 的字符串
此示例适用于 as400 中的 rpg(不是 rpgile)