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
在RPG IV中,我如何获取字符串并消除特定字符的所有实例或用另一个字符替换它们?这有点像其他programmnig语言中的内置方法中的字符串替换。例如:取021-123450-23-4并转换为021123450234
4 个解决方案
#1
The correct syntax for %xlate is:
%xlate的正确语法是:
%XLATE(from:to:string{:startpos})
To replace all hyphens with ampersands:
要用&符号替换所有连字符:
new_string = %xlate('-':'&':string);
new_string =%xlate(' - ':'&':string);
To remove all hyphens:
要删除所有连字符:
You cannot remove a character using &xlate. 7.1 gives us %scanrpl, but prior to that, use something like this:
您无法使用&xlate删除角色。 7.1给出了%scanrpl,但在此之前,请使用以下内容:
for i = 1 to %len(string);
char = %subst(string:i:1);
if char <> '-';
new_string += char;
endif;
endfor;
#2
Take a look at the following articles:
看看以下文章:
- http://www.itjungle.com/fhg/fhg030409-story01.html
- http://www.itjungle.com/fhg/fhg022509-story01.html
These should help.
这应该有所帮助。
#3
i had the same problem. so i wrote my own RPG procedure that does it for me:
我有同样的问题。所以我写了我自己的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
**
**
Since RPG is very picky about which column things are in, when you copy and reuse this code, you might need to adjust your pasted text, so there are 5 spaces before 'D*', '**', and 'P skReplace ...'. Six spaces are before '/free'. And all code between the '/free' lines have 7 or more spaces.
由于RPG对于哪些列的内容非常挑剔,因此当您复制和重用此代码时,您可能需要调整粘贴的文本,因此在“D *”,“**”和“P skReplace”之前有5个空格。 ..'。在'/ free'之前有六个空格。 '/ free'行之间的所有代码都有7个或更多空格。
I welcome any suggestions for improvement of this code. I also have procedures for Left, Right, and Mid if anyone wants them. Just msg me if you do. I'm happy to share them. I know RPG has a '%subst' function, but many programming languages are picky like they'll error if parameters are invalid. Mine instead give more flexibility, for instance, Left('aoeu', -1) returns 'aoe' (1 character short of the full string), and Right('aoeu', -1) returns 'oeu' (the right part of the string after 1 character is removed). My Mid procedure also allows a negative start position, indexing from the end of the string too, which i found to be useful to me. But they're free for anyone who wants to spend the time to ask me for them.
我欢迎任何改进此代码的建议。如果有人想要的话,我也有Left,Right和Mid的程序。如果你这样做,请告诉我。我很乐意与大家分享。我知道RPG有'%subst'功能,但是很多编程语言都很挑剔,如果参数无效就会出错。相反,我提供更多的灵活性,例如,Left('aoeu', - 1)返回'aoe'(1个字符短于完整字符串),Right('aoeu', - 1)返回'oeu'(右边部分)删除1个字符后的字符串)。我的Mid程序也允许负起始位置,也从字符串的结尾开始索引,我发现这对我有用。但对于任何想花时间问我的人来说,他们都是免费的。
Happy Coding!
#4
To remove a character you can use this
要删除角色,您可以使用此功能
strRes = %scanrpl('-':'':strSrc);
#1
The correct syntax for %xlate is:
%xlate的正确语法是:
%XLATE(from:to:string{:startpos})
To replace all hyphens with ampersands:
要用&符号替换所有连字符:
new_string = %xlate('-':'&':string);
new_string =%xlate(' - ':'&':string);
To remove all hyphens:
要删除所有连字符:
You cannot remove a character using &xlate. 7.1 gives us %scanrpl, but prior to that, use something like this:
您无法使用&xlate删除角色。 7.1给出了%scanrpl,但在此之前,请使用以下内容:
for i = 1 to %len(string);
char = %subst(string:i:1);
if char <> '-';
new_string += char;
endif;
endfor;
#2
Take a look at the following articles:
看看以下文章:
- http://www.itjungle.com/fhg/fhg030409-story01.html
- http://www.itjungle.com/fhg/fhg022509-story01.html
These should help.
这应该有所帮助。
#3
i had the same problem. so i wrote my own RPG procedure that does it for me:
我有同样的问题。所以我写了我自己的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
**
**
Since RPG is very picky about which column things are in, when you copy and reuse this code, you might need to adjust your pasted text, so there are 5 spaces before 'D*', '**', and 'P skReplace ...'. Six spaces are before '/free'. And all code between the '/free' lines have 7 or more spaces.
由于RPG对于哪些列的内容非常挑剔,因此当您复制和重用此代码时,您可能需要调整粘贴的文本,因此在“D *”,“**”和“P skReplace”之前有5个空格。 ..'。在'/ free'之前有六个空格。 '/ free'行之间的所有代码都有7个或更多空格。
I welcome any suggestions for improvement of this code. I also have procedures for Left, Right, and Mid if anyone wants them. Just msg me if you do. I'm happy to share them. I know RPG has a '%subst' function, but many programming languages are picky like they'll error if parameters are invalid. Mine instead give more flexibility, for instance, Left('aoeu', -1) returns 'aoe' (1 character short of the full string), and Right('aoeu', -1) returns 'oeu' (the right part of the string after 1 character is removed). My Mid procedure also allows a negative start position, indexing from the end of the string too, which i found to be useful to me. But they're free for anyone who wants to spend the time to ask me for them.
我欢迎任何改进此代码的建议。如果有人想要的话,我也有Left,Right和Mid的程序。如果你这样做,请告诉我。我很乐意与大家分享。我知道RPG有'%subst'功能,但是很多编程语言都很挑剔,如果参数无效就会出错。相反,我提供更多的灵活性,例如,Left('aoeu', - 1)返回'aoe'(1个字符短于完整字符串),Right('aoeu', - 1)返回'oeu'(右边部分)删除1个字符后的字符串)。我的Mid程序也允许负起始位置,也从字符串的结尾开始索引,我发现这对我有用。但对于任何想花时间问我的人来说,他们都是免费的。
Happy Coding!
#4
To remove a character you can use this
要删除角色,您可以使用此功能
strRes = %scanrpl('-':'':strSrc);