在第一个字符表达式或备注字段中搜索第二个字符表达式或备注字段,然后用第三个字符表达式或备注字段替换在第一个字符表达式或备注字段中每次出现的第二个字符表达式或备注字段。可以指定替换开始的位置和替换的次数。
- STRTRAN(cSearched, cExpressionSought [, cReplacement] [,nStartOccurrence] [, nNumberOfOccurrences] [, nFlags])
参数
cSearched
指定要搜索的字符表达式。
cSearched
可以是备注字段。
cExpressionSought
指定要在 cSearched 中搜索的字符表达式。该搜索区分大小写。
cExpressionSought 可以是备注字段。
[, cReplacement] 指定要替换 cSearched 中每次出现的 cSearchFor 的字符表达式。省略 cReplacement 将用空串替换每次出现的 cExpressionSought。
[, nStartOccurrence] 指定第一次要替换的出现的 cExpressionSought。
例如,如果 nStartOccurrence 为 4,则替换从 cSearched 中第四次出现的 cExpressionSought 开始,先三次出现的 cExpressionSought 保持不变。
省略 nStartOccurrence 默认的替换开始位置为第一次出现 cExpressionSought。
[, nNumberOfOccurrences] 指定要替换的 cExpressionSought 出现的数量。
省略 nNumberOfOccurrences 将从 nStartOccurrence 指定的出现位置开始,替换所有出现的 cExpressionSought。
[, nFlags] 依照下列值,指定搜索的大小写敏感性。
注意
如果只需要连同必要的参数一起指定 nFlags 设置,请对要忽略的可选参数指定 –1。
nFlags 值>
nFlags Value | Description |
0 (default) | Search is case-sensitive, replace is with exact cReplacement text. This is the behavior in the previous version of Visual FoxPro. |
1 |
Search is case-insensitive, replace is with exact cReplacement text. |
2 |
Search is case-sensitive, replace is with exact cReplacement text. |
3 |
Search is case-insensitive; case of cReplacement is changed to match the case of cExpressionSought, which it replaces. |
也可以通过将 –1 作为 nFlags 值传递来指定默认行为。
cReplacement 的大小写只是在串查找全部为大写、小写或特有的大小写时才更改。
示例
使用 STORE 命令将串 "abracadabra" 储存到变量 gcString。STRTRAN( ) 函数用字符 "z" 替换字符 "a",并用 ? 命令显示 "zbrzczdzbrz" 串。随后,STRTRAN( ) 从第二次出现开始用字符 "q" 替换三次出现的 "a" 字符,并显示 "abrqcqdqbra"。
- STORE 'abracadabra' TO gcString
- ? STRTRAN(gcString, 'a', 'z')
- ? STRTRAN(gcString, 'a', 'q', 2, 3)