Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin apply on String not working as expected

I am trying to use all features of kotlin, but seems not of them are working, or may be it's my fault.

So, apply to String not work. Example:

val str = someStr.apply {
    toUpperCase()
    if (contains("W")) replace("W", "w")
}

Input -> xywz

Output -> xywz

Expected -> XYwZ

Java style:

val str = it.text().toString().toUpperCase()
if (str.contains("W")) str.replace("W", "w")

Input -> xywz

Output -> XYwZ

Expected -> XYwZ

Am I doing something wrong?

like image 855
Anton A. Avatar asked Oct 14 '25 20:10

Anton A.


2 Answers

Actually apply does not return the value you calculated. You may rather want to use either: run, let or with. Additionally, probably more important, is that you do not specify an else path. That might return you a Unit instead of a value, so you may want to specify what should be returned otherwise. Finally, the calls within those methods are not chained. Calling toUpperCase first doesn't alter anything... It's nearly dead code... So you may want to write something like:

val str = with(someStr) {
  toUpperCase().run {
    if (contains("W")) replace("W", "w")
    else this
  }
}

However I just used run/let/with to demonstrate its usage, as you already used apply... your shown Java way is of course way easier in this regard and the simplest possible solution is rather the one TheOperator showed by just omitting your condition in the first place as the replace is case-sensitive by default.

like image 50
Roland Avatar answered Oct 17 '25 11:10

Roland


toUpperCase() returns a copy of the string (strings are immutable). So you need to store the returned value, as a stand-alone (not last) statement inside apply() it is lost.

Also, you can't use if without else, if you return an expression. The contains() is not even needed in your case.

What you probably would like to do is call toUpperCase() and replace() directly:

val str = someStr.toUpperCase().replace("W", "w")
like image 39
TheOperator Avatar answered Oct 17 '25 12:10

TheOperator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!