Scala - 大小写匹配部分字符串 [英] Scala - case match partial string

查看:51
本文介绍了Scala - 大小写匹配部分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几点:

serv match {

    case "chat" => Chat_Server ! Relay_Message(serv)
    case _ => null

}

问题是有时我还会在 serv 字符串的末尾传递一个额外的参数,所以:

The problem is that sometimes I also pass an additional param on the end of the serv string, so:

var serv = "chat.message"

有没有办法可以匹配字符串的一部分,这样它仍然会被发送到 Chat_Server?

Is there a way I can match a part of the string so it still gets sent to Chat_Server?

感谢您的帮助,非常感谢:)

Thanks for any help, much appreciated :)

推荐答案

使用正则表达式 ;)

Use regexes ;)

val Pattern = "(chat.*)".r

serv match {
     case Pattern(chat) => "It's a chat"
     case _ => "Something else"
}

使用正则表达式,您甚至可以轻松拆分参数和基本字符串:

And with regexes you can even easily split parameter and base string:

val Pattern = "(chat)(.*)".r

serv match {
     case Pattern(chat,param) => "It's a %s with a %s".format(chat,param)
     case _ => "Something else"
}

这篇关于Scala - 大小写匹配部分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆