scala获取字符串首字符和尾字符


scala获取字符串首字符和尾字符

获取第一个元素

take()

scala> "hello".take(1)
res12: String = h

索引法

因为字符串的本质是字符数组

scala> "hello"(0)
res13: Char = h

字符串截取

scala> "hello".substring(0,1)
res15: String = h

获取最后一个元素

先反转再获取第一个

scala> "Hello".reverse.take(1)
res11: String = o

takeRight()

scala> "hello".takeRight(1)
res14: String = o

字符串截取

scala> "hello".substring("hello".length-1)
res16: String = o

last函数截取

scala> "hello".last
res1: Char = o


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM