如何在 Golfscript 中找到字符串的索引?
给定一个字符串“ABCDE”,我如何在 Golfscript 中找到另一个字符串“C”的出现索引?
?运算符似乎不起作用(http://www.golfscript.com/golfscript/builtin .html#?):
“C”“ABCDE”?
Given a string "ABCDE", how do i find the index of occurrence of another string "C" in Golfscript?
? operator doesn't seem to work (http://www.golfscript.com/golfscript/builtin.html#?):
"C" "ABCDE" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
"C" "ABCDE" ?
不可能起作用 - 如果进行字符串搜索,它将在C 中查找
。ABCDE
的第一次出现然而,在 GolfScript 中,字符串实际上是整数数组的不同表示形式。
"ABCDE"67?
给出2
,因为 67 是C
的 Unicode 代码点。一种稍微好一点的方法,您可能期望工作但没有工作(X),
这是相当违反直觉的,但“正确”:
?
是一个顺序操作,字符串优先于数组。比较:第一个给出 2,如预期的那样,但第二个给出 -1,因为字符串的优先级意味着它正在字符串内搜索数组 - 并且没有数组会等于表示 Unicode 代码点的 int。然而,这些例子确实指出了在使用方法 X 之前将字符串减少为整数数组的另一种方法。
更新
我向 flagitious 发送了一封电子邮件,建议打补丁,最新版本的 Golfscript 对字符串有新的行为字符串? 和
字符串数组?
。因此,如果您更新,"ABCDE""C"?
应该给出2
。There's no way that
"C" "ABCDE" ?
would work - if that did a string search, it would be looking for the first occurrence ofABCDE
inC
.However, in GolfScript strings are really a different presentation of arrays of integers.
"ABCDE"67?
gives2
because 67 is the Unicode codepoint forC
.One slightly nicer approach which you might expect to work but doesn't is (X)
This is rather counter-intuitive, but "correct":
?
is an order operation, and string has priority over array. Compare:The first gives 2, as expected, but the second gives -1 because the priority of string means that it's searching for the array inside the string - and no array will ever be equal to an int representing a Unicode codepoint. However, these examples do point the way to another approach of reducing the strings to arrays of ints before using approach X.
Update
I sent an e-mail to flagitious suggesting a patch and the latest version of Golfscript has new behaviour for
string string ?
andstring array ?
. So if you update,"ABCDE""C"?
should give2
.