scala 如何使用 string func startsWith 检查字符串列表而不是一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/40959976/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use string func startsWith to check on a list of strings instead of one?
提问by JohnBigs
I have a list of names:
我有一个名字列表:
val listOfNames = List("john", "melanie", "maya", "Hyman")
and I have a string name full name:
我有一个字符串名称全名:
val fullName = "john legend"
and now I want to use the string func startsWithand check if the full name starts with any of those names in the listOfNames, so how can I check it in one line?
现在我想使用字符串 funcstartsWith并检查全名是否以 中的任何名称开头listOfNames,那么如何在一行中检查它?
something like
就像是
if (fullName.startsWith(listOfNames)) {
   //do something
}
thanks!!
谢谢!!
回答by Tim
Here's a concrete implementation that checks for any matches with the list of names:
这是一个具体的实现,用于检查与名称列表的任何匹配:
listOfNames.exists(firstName => fullName.startsWith(firstName))
回答by guilhebl
Another option:
另外一个选择:
listOfNames.exists(fullName.startsWith)
回答by Luftbaum
Simple: Iterate over your array and repeat the check for each one.
简单:遍历您的数组并为每个数组重复检查。

