问题描述
我想在 Rexx 中找到 字符串中第 n 次出现的字符。是否有任何内置功能可以做到这一点?如果没有,我该怎么做?
解决方法
使用 POS 功能 n 次。
例如,要找到字符串中的第三个 'a',请使用 pos("a",string,currentPos) 3 次,初始起点为 1,后续起点为前一个 pos 的结果。>
/* REXX */
string = "The quick brown fox jumps over the lazy dog"
srchchar = "e"
count = 2
say "The position of the '"count"' occurrence of '"srchChar"'"||," in '"string"' is at position : "
say findChar(srchChar,count)
exit
findChar: procedure
/* Find the nth (count) position of the string needle in haystack */
arg needle,haystack,count
currentPos = 0
do count
currentPos = currentPos + 1
currentPos = pos(needle,currentPos)
end
return currentPos
如果你运行这个,你会得到:
The position of the '2' occurrence of 'e' in 'The quick brown fox jumps over the lazy dog' is at position :
29
***
这将接受一个字符串作为搜索参数,而不仅仅是单个字符。如果您想强制搜索参数为单个字符,您可以,但我看不到这样做的任何价值,因为它会增加代码并减少功能。
例如
string = "The quick brown fox jumps over the lazy dog"
srchchar = "the"
count = 2
say "The position of the '"count"' occurrence of '"srchChar"'"||," in '"string"' is at position : "
say findChar(srchChar,count)
返回:
The position of the '2' occurrence of 'the' in 'The quick brown fox jumps over the lazy dog' is at position :
32
***
编辑 - 这是我的旧答案(由于我误读了任务而错误),它计算搜索字符出现在字符串中的次数。
我知道没有内置函数,但您可以编写一个子程序来逐个字符地遍历搜索字符串,将当前字符与您要查找的字符进行比较:
/* REXX */
string = "The quick brown fox jumps over the lazy dog"
srchchar = "q"
say "There is/are "count(srchchar,string)" occurrances of "||,"'"srchchar"' in '"string"'"
exit
count: procedure
arg needle,haystack
count = 0
do i = 1 to length(haystack)
currentChar = substr(haystack,i,1)
if currentChar = needle then count = count + 1
end
return count
我相信还有其他方法。
,这是一个函数的示例实现,用于返回大海捞针中第 n 次出现的位置。该函数名为 NthPositionOfNeedle
。
/*- REXX --------------------------------------------------------------------------
This function returns the position of the nth occurrence of a given character
in a given string. A negative return value indicates a parameter error,or
the fact that the nth occurrence was not found.
Parameters:
Haystack A non-empty string of characters to be search.
Needle A single character to be searched for in the haystack.
Count A whole number indicating the nth occurrence to be found.
Return values:
n where n > 0 indicates the position of the nth occurrence
of the needle within the haystack.
-1 The haystack is an empty string.
-2 The needle is not a single character.
-3 The count is not a whole number > 0.
-4 The needle was not found at all within the haystack.
-5 The nth occurrence was not found,but the needle was
found at least once.
---------------------------------------------------------------------------------*/
NthPositionOfNeedle: procedure
Haystack = arg(1)
Needle = arg(2)
Count = arg(3)
/* The haystack must not be an empty string. Return -1 if it is */
if length( Haystack ) = 0
then return -1
/* The Needle must be a single character. Return -2 if it is not */
if length( Needle ) <> 1
then return -2
/* The count must be a whole number > 0. Return -3 if it is not */
if datatype( Count,"W" ) = 0 | Count < 1
then return -3
/* Does needle exist at least once in the haystack? Return -4 if not */
if pos( Needle,Haystack ) = 0
then return -4
CurrCnt = 0
Start = 1
do while ( CurrCnt < Count & Start <= length( Haystack ) )
NeedlePos = pos( Needle,Haystack,Start )
/* No more occurrences? */
if NeedlePos = 0
then leave
CurrCnt = CurrCnt + 1
Start = NeedlePos + 1
end
/* Return the position of the nth occurrence,if so many exist,else return zero */
if CurrCnt = Count
then return NeedlePos
else return -5
这是测试上述功能的代码
/*- REXX --------------------------------------------------------------------------
Code to test the function "NthPositionOfNeedle".
---------------------------------------------------------------------------------*/
say "1st occurrence of 't' in 'this is a string with multiple characters i.' is",NthPositionOfNeedle( 'this is a string with multiple characters i.','t',1 )
/* ....+....1....+....2....+....3....+....4.... */
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is",'i',1 )
say "2nd occurrence of 'i' in 'this is a string with multiple characters i.' is",2 )
say "3rd occurrence of 'i' in 'this is a string with multiple characters i.' is",3 )
say "6th occurrence of 'i' in 'this is a string with multiple characters i.' is",6 )
say "7th occurrence of 'i' in 'this is a string with multiple characters i.' is",7 )
say "0th occurrence of 'i' in 'this is a string with multiple characters i.' is",0 )
say "2nd occurrence of 'x' in 'this is a string with multiple characters i.' is",'x',2 )
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is",NthPositionOfNeedle( '',1 )
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is",'',0 )
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is","a" )
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is",1.1 )
exit