Scala String can be used other than java.lang.String method

Geek Women Japan 2016 Advent Calendar 24th day pinch hitter article. I've summarized what I've been addicted to recently doing Scala.

About this article

Scala's String can use methods other than java.lang.String, so I will introduce it. There was almost no information in Japanese, so I will summarize it. (I checked if Ruby reverse is in Scala)

String in Scala

If you search for "Scala String" on Google, Hishidama's site will hit first. The first thing written there is:

Scala handles strings as java.lang.String. That is, it is a Java character string itself and is an immutable object.

If you read only this, it will look like "Scala String and Java String are the same". (* I'm sorry I finished reading here even though there is a continuation)

Methods that can be used with Scala String

The methods of the following classes can be used as the methods that can be used in Scala.

Execution result

scala> var hoge="scala"
hoge: String = scala

scala> hoge.reverse
res0: String = alacs

scala> hoge.length
res1: Int = 5

Why you can use the methods of two classes just by writing String

Because StringOps is defined as a function to automatically (implicitly) convert in Predef.

Reference link

-Scala string memo (Hishidama's Scala String Memo)

Recommended Posts

Scala String can be used other than java.lang.String method
Scala String can be used other than java.lang.String method
Ruby array methods that can be used with Rails (other than each)
Ruby array methods that can be used with Rails (other than each)
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
SwiftUI View that can be used in combination with other frameworks
[Rails] "pry-rails" that can be used when saving with the create method
Until ruby can be used on windows ...
Initial settings until S2Dao can be used
[Rails 6] method :: delete cannot be used with link_to
Organize methods that can be used with StringUtils
[Ruby] Methods that can be used with strings
Why Java String typeclass comparison (==) cannot be used