String weird behaviour

30 views
Skip to first unread message

Sandeep Kota

unread,
Aug 23, 2016, 12:50:41 PM8/23/16
to scala-user
Hi All,
In the following code snippet, I'm expecting the following outputs

scala> val numbers = "123456"
numbers: String = 123456

scala> numbers.foreach(n => println("Item: "+ (n+2)))


1) if it is taking 'n' as Integer 
Item: 3
Item: 4
Item: 5
Item: 6
Item: 7
Item: 8

2) if it is taking 'n' as char
Item: 12
Item: 22
Item: 32
Item: 42
Item: 52
Item: 62

But Unexpectedly I'm getting the following output.

Item: 51
Item: 52
Item: 53
Item: 54
Item: 55
Item: 56

Thanks,
Sandeep

Oliver Ruebenacker

unread,
Aug 23, 2016, 12:59:34 PM8/23/16
to Sandeep Kota, scala-user

     Hello,

  The character code for the character '1' is 49 according to ASCII and UTF-16, which is the encoding of Java/Scala strings.

     Best, Oliver

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Reply all
Reply to author
Forward
0 new messages