[Solved] String.substring not working properly

Hello Community. I am trying following code in Spark Core:

String tmp;
tmp = message.substring(commaPosition + 1, message.length());

It gives compilation error, but if I don’t assign anything to tmp then it works. For example below code compiles:

message.substring(commaPosition + 1, message.length());

So, am I missing somehting here? Any help will be highly appreciated.

Since Strings are objects, you are changing a reference and the methods for this currently don’t compile on Spark so they are commented out. I think @mdma has a plan to fix this in the future.

Concatenation does a copy and works every time:

tmp = "";  //optional: use to replace the value
tmp = tmp + message.substring(commaPosition + 1, message.length());
2 Likes

Hi bko,
Thanks for the reply. I tried it and it worked!

  • Ninad
1 Like