问题描述:

I'm trying to do something seemingly simple. I have meta-data with this format

 -key=value

I've already split the string at the = but I need to take the - off. I'm trying to use this function key.replaceFirst("-", ""); but it doesn't do anything to the string.

I've tried putting \\ in the regex but that solved nothing.

Solution:

I did not say key = key.replaceFirst("-", "");

网友答案:

You need to assign back return value of replaceFirst as String is immutable object:

key = key.replaceFirst("-", "");
网友答案:

String.replaceFirst does not replace the string in-place, but returns a replaced string.

You need to assign back the return value:

key = key.replaceFirst("-", "");
网友答案:

You are not assigning the string back to it !

key = key.replaceFirst("-", "");
System.out.println(key);

HTH, Keshava.

相关阅读:
Top