Remove Characters from the end of a String Scala
What is the simplest method to remove the last character from the end of a String in Scala?
I find Rubys String class has some very useful methods like chop. I would have used "oddoneoutz".headOption in Scala, but it is depreciated. I don't want to get into the overly complex:
string.slice(0, string.length - 1)
Please someone tell me there is a nice simple method like chop for something this common.
How about using dropRight, which works in 2.8:-
"abc!".dropRight(1)
Which produces "abc"
string.init // padding for the minimum 15 characters
val str = "Hello world!"
str take (str.length - 1) mkString
string.reverse.substring(1).reverse
That's basically chop, right? If you're longing for a chop method, why not write your own StringUtils
library and include it in your projects until you find a suitable, more generic replacement?
Hey, look, it's in commons.
If you want the most efficient solution than just use:
str.substring(0, str.length - 1)
참고URL : https://stackoverflow.com/questions/1822481/remove-characters-from-the-end-of-a-string-scala
'Programing' 카테고리의 다른 글
web.config 변환 프로세스에서 파일을 추가하는 방법은 무엇입니까? (0) | 2020.11.04 |
---|---|
코드 실행 속도 : ASP.NET-MVC 대 PHP (0) | 2020.11.04 |
PHP 배열 예제가 뒤에 쉼표를 남기는 이유는 무엇입니까? (0) | 2020.11.04 |
Razor View에서 POST 요청을받을 때 빈 문자열 대신 null이 표시되는 이유는 무엇입니까? (0) | 2020.11.04 |
XHR로드 완료 […] 로그 메시지 (0) | 2020.11.04 |