Scala 中的第二个参数的 indexOf
indexOf second argument in Scala
我想了解 Scala 中 indexOf
中的第二个参数对字符串意味着什么?
object Playground extends App {
val g: String = "Check out the big brains on Brad!"
println(g.indexOf("o",7));
}
上面的程序returns:25
这是我无法理解的原因?
实际上是最后一个o
的索引,但是它和7有什么关系呢?它是否像第二个参数 n
returns 第 n 个字符出现的索引,如果 n
超过出现次数,那么它 returns 最后出现的元素的索引?
但如果是这样的话,那么这就没有意义了:
object Playground extends App {
val g: String = "Check out the big brains on Brad!"
(1 to 7).foreach(i => println(s"$i th Occurence = ${g.indexOf("o",i)} "))
}
输出:
1 th Occurence = 6
2 th Occurence = 6
3 th Occurence = 6
4 th Occurence = 6
5 th Occurence = 6
6 th Occurence = 6
7 th Occurence = 25
来源:https://www.scala-exercises.org/std_lib/infix_prefix_and_postfix_operators
根据 Scala 字符串文档,第二个参数是开始搜索的索引:
def indexOf(elem: Char, from: Int): Int
Finds index of first occurrence of some value in this string after or at some start index.
elem : the element value to search for.
from : the start index
returns : the index >= from of the first element of this string that is equal (as determined by ==) to elem, or -1, if none exists.
因此,在您的情况下,当您指定 7 时,这意味着您将查找位于索引 7 或 之后的第一个字符“o”的索引.事实上,在您的字符串中有两个“o”,一个在索引 6 处,一个在索引 25 处。
indexOf(int ch, int fromIndex)
从指定索引 (fromIndex
) 查找字符串中的字符。这意味着它开始寻找第 7 个位置。
今后您需要学习阅读 official docs: indexOf:
def indexOf(elem: A, from: Int): Int
[use case] Finds index of first occurrence of some value in this general sequence after or at some start index.
Note: may not terminate for infinite-sized collections.
elem
the element value to search for.
from
the start index
returns
the index >= from of the first element of this general sequence that is equal (as determined by ==) to elem, or -1, if none exists.
个人比较喜欢用Intellij用CMD + B
跳转源码。
无论您如何看待:在您的开发流程中,您将经常访问您正在使用的库或语言的 manual\docs。
我想了解 Scala 中 indexOf
中的第二个参数对字符串意味着什么?
object Playground extends App {
val g: String = "Check out the big brains on Brad!"
println(g.indexOf("o",7));
}
上面的程序returns:25
这是我无法理解的原因?
实际上是最后一个o
的索引,但是它和7有什么关系呢?它是否像第二个参数 n
returns 第 n 个字符出现的索引,如果 n
超过出现次数,那么它 returns 最后出现的元素的索引?
但如果是这样的话,那么这就没有意义了:
object Playground extends App {
val g: String = "Check out the big brains on Brad!"
(1 to 7).foreach(i => println(s"$i th Occurence = ${g.indexOf("o",i)} "))
}
输出:
1 th Occurence = 6
2 th Occurence = 6
3 th Occurence = 6
4 th Occurence = 6
5 th Occurence = 6
6 th Occurence = 6
7 th Occurence = 25
来源:https://www.scala-exercises.org/std_lib/infix_prefix_and_postfix_operators
根据 Scala 字符串文档,第二个参数是开始搜索的索引:
def indexOf(elem: Char, from: Int): Int
Finds index of first occurrence of some value in this string after or at some start index.
elem : the element value to search for.
from : the start index
returns : the index >= from of the first element of this string that is equal (as determined by ==) to elem, or -1, if none exists.
因此,在您的情况下,当您指定 7 时,这意味着您将查找位于索引 7 或 之后的第一个字符“o”的索引.事实上,在您的字符串中有两个“o”,一个在索引 6 处,一个在索引 25 处。
indexOf(int ch, int fromIndex)
从指定索引 (fromIndex
) 查找字符串中的字符。这意味着它开始寻找第 7 个位置。
今后您需要学习阅读 official docs: indexOf:
def indexOf(elem: A, from: Int): Int
[use case] Finds index of first occurrence of some value in this general sequence after or at some start index.
Note: may not terminate for infinite-sized collections.
elem
the element value to search for.
from
the start index
returns
the index >= from of the first element of this general sequence that is equal (as determined by ==) to elem, or -1, if none exists.
个人比较喜欢用Intellij用CMD + B
跳转源码。
无论您如何看待:在您的开发流程中,您将经常访问您正在使用的库或语言的 manual\docs。