java 字符串部分替换,Java中,如何替换字符串部分字符

I have an array that keeps URL addresses in String format for me.

Before loading URLs, I need to make sure that there is no space in strings.

I wrote this code but ReplaceAll() doesn't change anything.

Please tell me what is wrong here?

public NewsFeedItemList getNewsList() {

String str;

for(int i=0; i

str = newsFeedItemList.getImage().get(i);

Log.i("before>>>", str);

str.replaceAll(" ", "%20");

Log.i("after<<<

newsFeedItemList.getImage().set(i, str);

}

return newsFeedItemList;

}

解决方案

Strings are immutable, meaning that you cannot change the contents of a String object. What you would need to do is something like

String cleanedString = str.replaceAll(" ", "%20");

The replaceAll()-Method returns the new String.

你可能感兴趣的:(java,字符串部分替换)