scala – 如何通过右边的分隔符拆分字符串?
发布时间:2020-12-16 08:50:39 所属栏目:安全 来源:网络整理
导读:如何用右边的分隔符拆分字符串? 例如 scala "hello there how are you?".rightSplit(" ",1)res0: Array[java.lang.String] = Array(hello there how are,you?) Python有一个.rsplit()方法,这是我在Scala中所追求的: In [1]: "hello there how are you?".rs
如何用右边的分隔符拆分字符串?
例如 scala> "hello there how are you?".rightSplit(" ",1) res0: Array[java.lang.String] = Array(hello there how are,you?) Python有一个.rsplit()方法,这是我在Scala中所追求的: In [1]: "hello there how are you?".rsplit(" ",1) Out[1]: ['hello there how are','you?'] 解决方法
我认为最简单的解决方案是搜索索引位置,然后根据它进行拆分.例如:
scala> val msg = "hello there how are you?" msg: String = hello there how are you? scala> msg splitAt (msg lastIndexOf ' ') res1: (String,String) = (hello there how are," you?") 因为有人在lastIndexOf上回复了-1,所以解决方案完全没问题: scala> val msg = "AstringWithoutSpaces" msg: String = AstringWithoutSpaces scala> msg splitAt (msg lastIndexOf ' ') res0: (String,String) = ("",AstringWithoutSpaces) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |