1

Possible Duplicate:
How to Force a jar to uses(or the jvm runs in) utf-8 instead of the system's default encoding

I have a bunch of Strings that I want to read in as UTF-8. Is there a way to force this encoding in Scala, without passing a command-line parameter?

For example, I want to do something like val utf8EncodedString = new String(myString, "UTF-8").

Community
  • 1
  • 1
grautur
  • 29,955
  • 34
  • 93
  • 128

1 Answers1

7

Strings do not have an encoding in Scala/Java. Internally, they are always saved as UTF-16, but that doesn't matter when you use them. Encoding and decoding happens in the IO classes. You should look at the documentation of the scala.io.Source object, which contains factory methods for creating Sources, which take the encoding as a parameter.

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
  • 3
    Hmm, so I tried using `Source.fromBytes(text.toLowerCase.getBytes(), "UTF-8").mkString` to convert my text into UTF-8, but that didn't work -- I'm probably still misunderstanding how to use `Source`. Do you know what I'm doing wrong? (To give some more context, what I'm trying to do is take Chinese text and be able to split it into individual Chinese characters.) – grautur Oct 11 '12 at 04:39