How to compare a string with another where the one has space in between

后端 未结 4 1255
挽巷
挽巷 2021-01-13 05:04

How do I compare these two string :

val a = \"fit bit versa\"
val b = \"fitbit\"

another example

val a = \"go pro hero 6\"
         


        
4条回答
  •  礼貌的吻别
    2021-01-13 05:41

    Something like this I guess (your requirements are incomplete, so I interpreted them to "match exactly the beginning portion of the given string, ending with whitespace or end of line, except maybe spaces).

      @tailrec
      def matchWords(input: Seq[Char], words: Seq[Char]): Boolean = (input, words) match {
         case (Seq(), Seq() | Seq(' ', _*)) => true
         case (Seq(), _) => false
         case (Seq(a, tail@_*), Seq(b, rest@_*)) if a == b => matchWords(tail, rest)
         case (_, Seq(' ', rest@_*)) => matchWords(input, rest)
         case _ => false
       }
    

提交回复
热议问题