Scala:删除最后一个字符 [英] Scala: Remove the last occurrence of a character

查看:489
本文介绍了Scala:删除最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除字符串中最后一个字符。我可以得到索引

I am trying to remove the last occurrence of a character in a string. I can get its index:

str.lastIndexOf(',')

我已经尝试使用拆分和字符串上的替换函数。

I have already tried to use split and the replace function on the string.

推荐答案

你可以用 patch

scala> val s = "s;dfkj;w;erw"
s: String = s;dfkj;w;erw

scala> s.patch(s.lastIndexOf(';'), "", 1)
res6: String = s;dfkj;werw

这篇关于Scala:删除最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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