0

Replace a pattern in a long String. Used replace method in a loop, but every time it will replace the first occurrence.

I want to Iterate and replace all occurrences one by one for example

LongString.contains("/ima?pth=") 
LongString=LongString.replace("/ima?pth=", "/ima?pth=**1**");

Every time I have to add a new number at the end of the occurrence it can be any value. How to do this in Java?

0

4 Answers 4

0

According to String.replaceAll without RegEx it should replace all... Otherwise you could put in a while loop. I caution this though, as LongString.contains("/ima?pth=") would still be true when it is all replaced. Would be easy to have an infinite loop.

Sign up to request clarification or add additional context in comments.

4 Comments

replace will everytime replace first occurance
I want to iterate through the string match pattern replace it with "/ima?pth=**1**"
String newBody=body.replace("<img src=", "<img src=\"cid:"+n+""); n is any random number. Everytime it will replace the first occurance of <img src which i donot want , there are many <img drc tags to be replaced
Ah.. I misunderstood the question. I would change your "contains" to include what follows the "<img src=" when it is first blank. Then you can keep a counter in the while(could also do a for loop) and replace with that variable LongString=LongString.replace("/ima?pth=", "/ima?pth=" + varName);
0

You could use a StringBuilder like so:

class Main {
  public static void main(String[] args) {
    String longString = "qwe/ima?pth=asdzxc/ima?pth=qweasd/ima?pth=sdf";
    StringBuilder str = new StringBuilder(longString);
    String pattern = "/ima?pth=";
    int index = longString.indexOf(pattern);
    int number = 1;
    while(index >= 0) {
     str.insert(index + pattern.length(), number);
     index = str.indexOf("/ima?pth=", index+1);
     number += 1;
    }
    longString = str.toString();
    System.out.println(longString); //qwe/ima?pth=1asdzxc/ima?pth=2qweasd/ima?pth=3sdf
  }
}

You could modify the operations on number as desired but in this instance it only goes up by 1.

Try it here!

Comments

0

Use Matcher and Matcher#appendReplacement()

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReplaceAllEx {

    public static void main(String[] args) {
        String input = "randomPrefix"
                + "/ima?pth=randomInfix/ima?pth="
                + "/ima?pth=randomInfix/ima?pth="
                ;
        Pattern p = Pattern.compile("/ima\\?pth=");
        Matcher m = p.matcher(input);
        int i = 1;
        StringBuffer sb = new StringBuffer();
        System.out.println("input is\n" + input + "\n");
        while (m.find()) {
            m.appendReplacement(sb, "/ima?pth="+"**"+i+"**");
            i++;
        }
        System.out.println("output is\n" + sb.toString() + "\n");
    }

}

The output of the above program would be:

input is
randomPrefix/ima?pth=randomInfix/ima?pth=/ima?pth=randomInfix/ima?pth=

output is
randomPrefix/ima?pth=**1**randomInfix/ima?pth=**2**/ima?pth=**3**randomInfix/ima?pth=**4**

Updated

Here is another example demonstrating the use of Matcher#appendReplacement() -- with OP's new input example.

private static void example2() {
    String input = "<img src=\"cid:1\"/>"
    private static void example2() {
        String input = "<img src=\"cid:1\"/>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "</span><div>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "<br /></span></div>"
//              + "<div>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "<br /></span></div>"
//              + "<div>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "</span><br /><div style=\"display:none;\">"
//              + "</div></div><div>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "<br /></span></div><div>"
                + "<img src=\"/XP/image?path=09021b5e80061f2c\"/>"
//              + "<span style=\"font-size:14pt;font-family:'Times New Roman';\">"
//              + "<br /></span>"
                + "<img src=\"/XP/image?path=09021b5e80061f2c\"/>"
                + "<img src=\"/XP/image?path=09021b5e80061f2c\"/>"
                ;
        Pattern p = Pattern.compile("<img src=\\\"/XP/image\\?path=09021b5e80061f2c\\\"/>");
        Matcher m = p.matcher(input);
        int i = 2;
        StringBuffer sb = new StringBuffer();
        System.out.println("input is\n" + input + "\n");
        while (m.find()) {
            String replacement = String.format("<img src=\"cid: %d\"/>", i);
            m.appendReplacement(sb, replacement);
            i++;
        }
        System.out.println("output is\n" + sb.toString() + "\n");

    }

This would be the program output:

input is
<img src="cid:1"/><img src="/XP/image?path=09021b5e80061f2c"/><img src="/XP/image?path=09021b5e80061f2c"/><img src="/XP/image?path=09021b5e80061f2c"/>

output is
<img src="cid:1"/><img src="cid: 2"/><img src="cid: 3"/><img src="cid: 4"/>

6 Comments

<img src="cid:1"/><span style="font-size:14pt;font-family:'Times New Roman';"></span><div><span style="font-size:14pt;font-family:'Times New Roman';"><br /></span></div><div><span style="font-size:14pt;font-family:'Times New Roman';"><br /></span></div><div><span style="font-size:14pt;font-family:'Times New Roman';"></span><br /><div style="display:none;"></div></div><div><span style="font-size:14pt;font-family:'Times New Roman';"><br /></span></div><div><img src="/XP/image?path=09021b5e80061f2c"/><span style="font-size:14pt;font-family:'Times New Roman';"><br /></span>
I was able to replace the first occurance of string with <img src="cid:1"/>,
But how can i replace all the occurances. <img src="/XP/image?path=09021b5e80061f2c"/> with <img src="cid:1"/> 2 , 3 ,4 so on..
@Jimmy_R -- In your first comment above, there seems to be only one occurrence of <img src="/XP/image?path=09021b5e80061f2c"/>. Anyway, if you think my answer is pointing you in the right direction. Please upvote it and accept it. I can see if I can post an update to the answer to address your new example.
leeyuiwah- I did . thanks for the help , Voted up too :) :)
|
0

replaceAll(String regex, String replacement)

Should have you covered

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.