I need to do a char conversion like this:
accountNumber => ACCOUNT_NUMBER
Where there is a caps letter, then prefix a underscore. If not, just capitalize the character
I tried like below
scala> "accountNumber".map{ x => x match { case x if x == x.toUpper => "_"+x ; case x if x!= x.toUpper => x.toUpper}}.mkString
res38: String = ACCOUNT_NUMBER
It works, but it works differently when there is a digit in between.
scala> "filler51".map{ x => x match { case x if x == x.toUpper && x.isDigit && true => "_"+x ; case x if x!= x.toUpper => x.toUpper}}.mkString
res31: String = FILLER_5_1
I'm expecting it to print FILLER51. For this, I tweaked the code as below, but I'm getting an error.
scala> "filler51".map{ x => x match { case x if x == x.toUpper && !(x.isDigit) => "_"+x ; case x if x!= x.toUpper => x.toUpper}}.mkString
scala.MatchError: 5 (of class java.lang.Character)
at .$line40$$read$$$anonfun$1(<console>:12)
at .$line40$$read$$$anonfun$1$adapted(<console>:12)
at $$Lambda$1399/645018917.apply(Unknown Source)
at ap(StringOps.scala:29)
... 28 elided