8

I have a String in my Scala program that I'd like to cast as an Int.

def foo(): Int = x.getTheNumericString().toInt

The problem is that x.getTheNumericString() comes from a Java library and returns a java.lang.String, which doesn't have a toInt method.

I know I can create a Scala string with val s: String = "123", but I noticed that when I create a string like val t = "456" I get a java.lang.String. I have heard that Scala String is just a wrapper around java.lang.String, but I haven't found any clear documentation on how to cast to the Scala string.

Is there some function I can use like:

def foo(): Int = f(x.getTheNumericString()).toInt

As it stands now, my compiler complains about the original definition value toInt is not a member of String

Community
  • 1
  • 1
munk
  • 12,340
  • 8
  • 51
  • 71

3 Answers3

11

It's not a wrapper, but actually java.lang.String. No need in additional hassle:

» touch 123
» scala
...

val foo = new java.io.File("123")
// java.io.File = 123

// Get name is a java api, which returns Java string

foo.getName.toInt
// res2: Int = 123
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • 1
    To back this up, check the source of Predef out: https://github.com/scala/scala/blob/v2.11.1/src/library/scala/Predef.scala#L85 – gpampara May 22 '14 at 15:00
  • 1
    @gpampara this will require an additional comment, stating that `type` will be substituted at compile time and [leaves no actual traces in resulting bytecode](http://stackoverflow.com/q/13673839/298389) – om-nom-nom May 22 '14 at 15:02
  • I wish that were true, but my compiler complains that `toInt is not a member of String` – munk May 22 '14 at 15:10
  • @usmcs which version of Scala are you using? – om-nom-nom May 22 '14 at 15:19
  • 2.10.1 in Intellij 13.0.1 – munk May 22 '14 at 15:27
  • Try to compile it with SBT, it can happen that IDEA compiler misses things up like methods, the only source of truth for me is the Scala compiler when I'm in doubt. – Ende Neu May 22 '14 at 15:34
  • I have the same result with SBT `myfile.scala:13 value toLong is not a member of String` – munk May 22 '14 at 15:48
  • @usmcs [I set up basic project](https://gist.github.com/lazyval/90027f227114d48aa080), which makes use of scala 2.10.1 and java api, returning String. As it was expected it works just fine. There must be something else you're not saying us. You can check out that project by yourself by cloning it (git link is on the left) and running `sbt run` in it's directory – om-nom-nom May 22 '14 at 16:24
  • [Here is the IDEA result](http://take.ms/9hqrl). No compile errors, no runtime errors, no error highlighting. Sbt one looks essentially the same – om-nom-nom May 22 '14 at 16:32
  • @om-nom-nom The project works perfectly for me. I'll go back to my project and try to find out what is different. – munk May 22 '14 at 17:19
  • I found it, there was a stray import of `scala.predef.String`. Thank you! – munk May 22 '14 at 17:25
4

java.lang.String is implicitly amended with Scala specific string methods so no manual conversion should be necessary.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
0

Use asInstanceOf[String] to syntactically convert Java string to Scala string.

val str: String = "java_string".asInstanceOf[String]
Sameer
  • 101
  • 2
  • 11