在 Rexx 中查找第 n 个出现的字符
Posted
技术标签:
【中文标题】在 Rexx 中查找第 n 个出现的字符【英文标题】:Find nth occurance of character in Rexx 【发布时间】:2021-07-20 08:58:46 【问题描述】:我想在 Rexx 中找到 字符串中第 n 次出现的字符。是否有任何内置功能可以做到这一点?如果没有,我该怎么做?
【问题讨论】:
您可以使用 pos 函数 (POS(needle, haystack [,start])
) 在某个位置之后查找第一次出现的字符串。将 pos 函数与 do while 循环 结合使用
【参考方案1】:
使用 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, string, 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, haystack, 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, string, 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
我相信还有其他方法。
【讨论】:
当然还有其他方法,特别是如果您想解决 OPs 问题 :-) 他要求找到一种方法来查找字符的第 n 次出现,而不是该字符的出现次数。 是的 - 设法误读了这个问题。我会编辑我的答案。谢谢。【参考方案2】:这里是一个函数的示例实现,用于返回大海捞针中第 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" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 1 )
say "2nd occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 2 )
say "3rd occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 3 )
say "6th occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 6 )
say "7th occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 7 )
say "0th occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 0 )
say "2nd occurrence of 'x' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'x', 2 )
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( '', 'i', 1 )
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', '', 1 )
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 0 )
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', "a" )
say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 1.1 )
exit
【讨论】:
【参考方案3】:解析很好。通常保持简短。这是在此 Q 的上下文中使用它的一种方式:
/* REXX: PosOfNth: Position of nth character(string) in haystack */
/* tests */
s='Foo Bar Baz'; c='F'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)
s='Foo Bar Baz'; c='a'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)
s='Foo Bar Baz'; c='a'; n=2; Say n'-th 'c' in 's"="PosOfNth(s,c,n)
s='Foo Bar Baz'; c='z'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)
s='Foo Bar Baz'; c='z'; n=2; Say n'-th 'c' in 's"="PosOfNth(s,c,n)
s='Lorem Ipsum'; c='m'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)
s='Lorem Ipsum'; c='z'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)
s='Lorem Ipsum'; c='Ips'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)
/* "visual" test */
Say
s='Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod...'
c='a'; n=2
Say s; Say Left('',PosOfNth(s,c,n)-1,' ')||'^ - 'n'-th 'c' is here'
Exit
/* ===== PROCS ===== */
PosOfNth: Procedure ;
Parse Arg haystack, needle, n
posN = 0
Do n
oHaystack = haystack
Parse Value haystack With head (needle) haystack
If haystack='' & oHaystack<>head||needle Then Do
posN = 0
Leave
End; Else posN = posN + Length(head) + 1
End
posN = posN + (Length(needle)-1) * (n-1)
Return posN
编辑
正如@phunsoft 指出的那样,多字符针(搜索字符串)存在问题。我添加了行posN = posN + (Length(needle)-1) * (n-1)
来调整它。
【讨论】:
当搜索字符串只出现一次时,您的代码将失败,并且当搜索字符串长于一个字符时,视觉位置指示 (^) 会关闭一个位置。不过,这不是解析的问题。 哦,我明白你的意思了 - 位置指示器向左移动,因为当它大于 1 & n>1 时,它不考虑针的长度。我添加了一个编辑来解决它。以上是关于在 Rexx 中查找第 n 个出现的字符的主要内容,如果未能解决你的问题,请参考以下文章