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