scala string.split 不起作用 [英] scala string.split does not work

查看:33
本文介绍了scala string.split 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的 REPL 输出.我不确定为什么 string.split 在这里不起作用.

Following is my REPL output. I am not sure why string.split does not work here.

val s = "Pedro|groceries|apple|1.42"
s: java.lang.String = Pedro|groceries|apple|1.42

scala> s.split("|")
res27: Array[java.lang.String] = Array("", P, e, d, r, o, |, g, r, o, c, e, r, i, e, s, |, a, p, p, l, e, |, 1, ., 4, 2)

推荐答案

如果您使用引号,则是在要求对正则表达式进行拆分.| 是或"字符,因此您的正则表达式不匹配或不匹配.所以一切都分裂了.

If you use quotes, you're asking for a regular expression split. | is the "or" character, so your regex matches nothing or nothing. So everything is split.

如果你使用 split('|')split("""\|""") 你应该得到你想要的.

If you use split('|') or split("""\|""") you should get what you want.

这篇关于scala string.split 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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