Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用递增值替换字符串_Java_Parsing - Fatal编程技术网

Java 用递增值替换字符串

Java 用递增值替换字符串,java,parsing,Java,Parsing,我有一个ArrayList,它是根据论坛中的帖子构建的,每个条目都是一篇帖子。 当我构建条目时,我用字符串“Image[x]”替换所有元素。 填充数组列表后,我想返回并用递增整数替换“[x]”的所有实例 我正在使用以下代码构建阵列列表: Elements wholePosts = doc.select("div.post_body"); for (Element wholePost : wholePosts) { Elements texts =

我有一个ArrayList,它是根据论坛中的帖子构建的,每个条目都是一篇帖子。 当我构建条目时,我用字符串“Image[x]”替换所有元素。 填充数组列表后,我想返回并用递增整数替换“[x]”的所有实例

我正在使用以下代码构建阵列列表:

Elements wholePosts = doc.select("div.post_body");
            for (Element wholePost : wholePosts) {
            Elements texts = wholePost.select("div[itemprop=commentText]");
            for (Element text : texts) {

                String nobr = text.html().replaceAll("(?i)<br[^>]*>", "newlineplaceholder");
                String formatted1 = nobr.replaceAll("<img src=.*?>", "Image[x]");
                Document finalPost = Jsoup.parse(formatted1);
                String almostfinalText = finalPost.text();
                String finalText = almostfinalText.replace("newlineplaceholder", "\n");


                datumList.add(finalText);

            }
Elements wholePosts=doc.select(“div.post_body”);
for(元素wholePost:wholePosts){
Elements text=wholePost.select(“div[itemprop=commentText]”;
用于(元素文本:文本){
字符串nobr=text.html();
字符串formatted1=nobr.replaceAll(“,”Image[x]”);
documentfinalPost=Jsoup.parse(formatted1);
字符串almostfinalText=finalPost.text();
字符串finalText=almostfinalText.replace(“newlineplaceholder”,“\n”);
datumList.add(finalText);
}

我曾尝试在上面的代码中替换字符串并增加它,但它只对每个post元素增加,因此如果一篇文章中有多个图像,post 1将包含“Image1,Image1”,post 2将包含“Image2,Image2”。我要找的是post 1包含“Image1,Image2”,post 2包含“Image3,Image4”

替换“[x]”的代码在哪里?您需要一次替换一个“[x]”,而不是在给定字符串中一次替换所有“[x]”。创建一个初始化为1的
int imageCounter
变量,将其字符串表示形式嵌入到图像字符串中,然后递增。您可以立即执行此操作(正如代码大师已经建议的那样)以后不需要进行任何替换。我尝试了多种方法将其替换为一个整数值,问题是,对于给定数组列表索引中的图像的每个实例,我最终的整数都保持不变。下面是我尝试的最新位;for(int I=0;I