在peoplecode里想用跟Oracle一样的replace函数,进行字符串替换。
peoplecode里确实也有replace函数,但是这个函数的功能和Oracle的replace还不太一样。
我们来看看peoplecode的replace函数,就是直接将某段字符串替换为新字符串,这并不是我想要的。
Replace
Syntax
Replace(oldtext, start, num_chars, newtext)
Description
Use the Replace function to replace a specified number of characters in a string.
Parameters
Field or Control | Definition |
---|---|
oldtext | A String value, part of which is to be replaced. |
start | A Number designating the position in oldtext from which to start replacing characters. |
num_chars | A Number, specifying how many characters to replace in oldtext. |
newtext | A String value that replaces num_chars characters. |
Returns
Returns a String value in which specific characters in oldtext are replaced with newtext.
Example
After the following statement &NEWDATESTR equals "1997":
&NEWDATESTR = Replace("1996",3,2,"97");
If this example, where the number of characters in newtext is less than num_chars, &SHORTER equals "txtx":
&SHORTER = Replace("txt123",4,3,"x");
In this example, where the number of characters in newtext is greater than num_chars, &LONGER equals "txtxxxx":
&LONGER = Replace("txt123",4,3,"xxxx");
substitue才是我想要的,类似Oracle的replace的函数,用法也类似,将原字符串替换成目标字符串。
Substitute
Syntax
Substitute(source_text, old_text, new_text)
Description
Use the Substitute function to replace every occurrence of a substring found in a string with a new substring. To replace text that occurs in a specific location in a text string use the Replace function.
Parameters
Field or Control | Definition |
---|---|
source_text | A String in which you want to replace substrings. |
old_text | A String equal to the substring of source_text you want to replace. A tilde character (~) used in the old_text parameter stands for an arbitrary number of white spaces. For example, the following substitution: Substitute("2003* 0723* * * * ~", "* ~", "~") produces the result 2003~0723~~~~~, not the result 2003* 0723* * * ~. |
new_text | A String with which to replace occurrences of old_text in source_text. |
Returns
Returns a String resulting from replacing every occurrence of old_text found in source_text with new_text.
Example
The following example changes "Second Annual Conference" to "Third Annual Conference":
&newstr = Substitute("Second Annual Conference","Second","Third");
The next example sets &newstr to "cdcdcd":
&newstr = Substitute("ababab", "ab", "cd");