Ibm midrange RPG-IV中的字符串替换方法
在RPG IV中,如何获取字符串并消除特定字符的所有实例,或用另一个实例替换它们?。这有点像其他编程语言中的字符串替换内置方法。例如:以021-123450-23-4为例,将其转换为02123450234查看以下文章:Ibm midrange RPG-IV中的字符串替换方法,ibm-midrange,rpgle,Ibm Midrange,Rpgle,在RPG IV中,如何获取字符串并消除特定字符的所有实例,或用另一个实例替换它们?。这有点像其他编程语言中的字符串替换内置方法。例如:以021-123450-23-4为例,将其转换为02123450234查看以下文章: 这些应该会有所帮助。请看以下文章: 这些应该会有帮助。%xlate的正确语法是: %XLATE(从:到:字符串{:startpos}) 要用符号和替换所有连字符,请执行以下操作: 新字符串=%xlate(“-”:“&”:字符串) 要删除所有连字符,请执行以下操作:
这些应该会有所帮助。请看以下文章:
这些应该会有帮助。%xlate的正确语法是: %XLATE(从:到:字符串{:startpos}) 要用符号和替换所有连字符,请执行以下操作: 新字符串=%xlate(“-”:“&”:字符串) 要删除所有连字符,请执行以下操作: 不能使用&xlate删除字符。7.1为我们提供了%scanrpl,但在此之前,请使用以下内容:
for i = 1 to %len(string);
char = %subst(string:i:1);
if char <> '-';
new_string += char;
endif;
endfor;
i=1到%len(字符串)的;
char=%subs(字符串:i:1);
如果字符'-';
新字符串+=字符;
endif;
endfor;
%xlate的正确语法是:
%XLATE(从:到:字符串{:startpos})
要用符号和替换所有连字符,请执行以下操作:
新字符串=%xlate(“-”:“&”:字符串)
要删除所有连字符,请执行以下操作:
不能使用&xlate删除字符。7.1为我们提供了%scanrpl,但在此之前,请使用以下内容:
for i = 1 to %len(string);
char = %subst(string:i:1);
if char <> '-';
new_string += char;
endif;
endfor;
i=1到%len(字符串)的;
char=%subs(字符串:i:1);
如果字符'-';
新字符串+=字符;
endif;
endfor;
我也有同样的问题。因此,我编写了自己的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个或更多空格
我欢迎任何改进此代码的建议。
如果有人需要,我也有左、右和中的程序。只要给我发短信就行了。我很高兴与大家分享。我知道RPG有一个“%subst”函数,但许多编程语言都很挑剔,好像如果参数无效就会出错。相反,Mine提供了更大的灵活性,例如,左('aoeu',-1)返回'aoe'(比完整字符串短1个字符),右('aoeu',-1)返回'oeu'(删除1个字符后字符串的右侧部分)。我的Mid过程还允许一个负的开始位置,从字符串的末尾开始索引,我发现这对我很有用。但是他们是免费的,任何人都可以花时间向我索要
快乐编码 我也有同样的问题。因此,我编写了自己的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个或更多空格
我欢迎任何改进此代码的建议。
如果有人需要,我也有左、右和中的程序。只要给我发短信就行了。我很高兴与大家分享。我知道RPG有一个“%subst”函数,但许多编程语言都很挑剔,好像如果参数无效就会出错。相反,Mine提供了更大的灵活性,例如,左('aoeu',-1)返回'aoe'(比完整字符串短1个字符),右('aoeu',-1)返回'oeu'(删除1个字符后字符串的右侧部分)。我的Mid过程还允许一个负的开始位置,从字符串的末尾开始索引,我发现这对我很有用。但是他们是免费的,任何人都可以花时间向我索要
快乐编码 要删除字符,可以使用以下命令
strRes = %scanrpl('-':'':strSrc);
要删除角色,可以使用以下命令
strRes = %scanrpl('-':'':strSrc);