Java中,如何替换字符串部分字符 [英] Java, how to replace some characters in String

查看:1654
本文介绍了Java中,如何替换字符串部分字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有保留的URL地址以String格式为我的数组。
加载的URL之前,我需要确保有在字符串中没有空间。
我写这篇code,但的replaceAll()不会改变任何东西。

请告诉我什么是错在这里?

 公共NewsFeedItemList getNewsList(){
        字符串str;        的for(int i = 0; I< newsFeedItemList.getImage()大小();我++){
            。STR = newsFeedItemList.getImage()获得(I)
            Log.i(前>>>中,STR);
            str.replaceAll(,%20);
            Log.i(之后<<<<,STR);
            newsFeedItemList.getImage()集(I,STR)。
        }        返回newsFeedItemList;
    }


解决方案

字符串是不可变的,这意味着你不能改变一个String对象的内容。你需要做的是一样的东西。

 字符串cleanedString = str.replaceAll(,%20);

本的replaceAll() - 方法返回新的String

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<newsFeedItemList.getImage().size(); i++){
            str = newsFeedItemList.getImage().get(i);
            Log.i("before>>>", str);
            str.replaceAll(" ", "%20");
            Log.i("after<<<<", str);
            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中,如何替换字符串部分字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆