我有一个条件,要求我有2个如下所示的字符串
val data1 = ("42881644070,13.04148,80.096043,32,2019-05-01 12:32:00,[32435381]#42881685433,13.057927,80.127096,45,2019-05-01 12:32:00,[32435383]#42881685434,13.057927,80.127096,35,2019-05-01 12:32:00,[32435384]")
val data2 = ("42881644070#43848058544#43847944680#43849252675#43849251662#43849943435#43850472762")
我想将数据2的值与data1第一个ID为ID的项目进行比较,并且项目之间的分隔符为#。此外,输出应仅是与data2不匹配的项目。
此处输出应为Array [String]
Array(42881685433,13.057927,80.127096,45,2019-05-01 12:32:00,[32435383], 42881685434,13.057927,80.127096,35,2019-05-01 12:32:00,[32435384])
答案 0 :(得分:1)
这是您可以做的:
val data1 = "42881644070,13.04148,80.096043,32,2019-05-01 12:32:00,[32435381]#42881685433,13.057927,80.127096,45,2019-05-01 12:32:00,[32435383]#42881685434,13.057927,80.127096,35,2019-05-01 12:32:00,[32435384]"
val data2 = "42881644070#43848058544#43847944680#43849252675#43849251662#43849943435#43850472762"
val array1 = data1.split("#")
val array2 = data2.split("#")
val res = array1.filterNot(elem => array2.contains(elem.split(",").head))
println(res.mkString(","))
// Displays
// 42881685433,13.057927,80.127096,45,2019-05-01 12:32:00,[32435383],42881685434,13.057927,80.127096,35,2019-05-01 12:32:00,[32435384]