0

Given a String:

val myString: Any = "baa baa bLack shEEp"

I would like to have a new string (as myString is immutable of course) containing:

BAA BAA BLACK SHEEP

Checked in https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-upper-case.html But it says it is deprecated. I must say - it works, but probably there's a reason for that. Any idea?

3
  • Why Any? Cant you use String as type, or not declate type so its inferred from the value?
    – cutiko
    Nov 17, 2021 at 20:35
  • It's deprecated because the locale used is ambiguous.
    – Tenfour04
    Nov 17, 2021 at 20:58
  • The docs literally tell you to use uppercase() instead! Nov 18, 2021 at 16:08

2 Answers 2

1

simple you just need to do this:

val myString = "baa baa bLack shEEp" 
println(myString.uppercase())
0

the method toUpperCase() is indeed deprecated. here's an example how to achieve upper case:

val myString: Any = "baa baa bLack shEEp"
if (myString is String){
    println(myString.uppercase())
}
1
  • 1
    You really don't need the Any type, and thus the type check. Just use val myString = "your literal" and it will already be of the correct type
    – Joffrey
    Nov 17, 2021 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.