Scala - case match partial string

后端 未结 3 1818
迷失自我
迷失自我 2021-02-01 14:50

I have the following:

serv match {

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

}

The problem is that some

3条回答
  •  一个人的身影
    2021-02-01 15:20

    Have the pattern matching bind to a variable and use a guard to ensure the variable begins with "chat"

    // msg is bound with the variable serv
    serv match {
      case msg if msg.startsWith("chat") => Chat_Server ! Relay_Message(msg)
      case _ => null
    }
    

提交回复
热议问题