我有一个变量定义为:
D content 1280A CONST
我需要找到"
, &
, '
, <
,>
字符并将它们替换为:
"
, &
, '
, <
, 和>
; 分别。
我在该语言中看到了一些 XML 函数,但这些似乎不是我需要的。但我可能是错的,所以我在这里问。
使用 RPGLE,自由形式。
解决方案:也许不是很 RPG 风格,但它确实有效
P encode B
D PI 1280A
D content 1280A CONST
D outStr S 1280A
D strDsp S 50A
/free
outStr = %trim(content);
outStr = replaceAll('&' : '&' : outStr);
outStr = replaceAll('"' : '"' : outStr);
outStr = replaceAll('''' : ''' : outStr);
outStr = replaceAll('>' : '>' : outStr);
outStr = replaceAll('<' : '<' : outStr);
return outStr;
/end-free
P E
P*** Procedure: replaceAll ************************************
P*** IN: character to replace, replacement text, source
P*** OUT: transformed string
P replaceAll B
D PI 1280A
D character 1A CONST
D rText 10A CONST
D content 1280A CONST
D outStr S 1280A
D dspStr S 50A
D rSize S 3P 0 //replacement text size
D index S 3P 0 //cur str index
D cntSize S 3P 0 //content size
/free
rSize = %len(%trim(rText));
cntSize = %len(%trim(content));
outStr = content;
for i = 1 to cntSize; //scan starts at character 1 not 0
index = %scan(character : outStr : i);
if index = 0;
leave;
endif;
outStr = %replace(%trim(rText) : outStr : index : 1);
i = index + 1;
endfor;
return outStr;
/end-free
P E