-3

I have a url which is generate on the fly and i want to place some text with unknowing text using string builder. Please let me know how?

Example:-

http://localhost/abcdef/servlet/cpd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abcdef&lang=ENG&val=PRCTXT|ABCDE_-1223344&classGroupid=101,201&fgcolor=000000&bgcolor=E0DBD8&width=100&height=80&fontSize=11&fontWeight=normal.

The above URL is a string builder and "val=PRCTXT|ABCDE_-1223344" text has to change with "val=123456" text. But here Val is always user input . so it is changing always.

RKCY
  • 4,095
  • 14
  • 61
  • 97
  • Can you use `String.split()`? – Frecklefoot Aug 03 '16 at 19:26
  • 1
    Why are you focused on `StringBuilder`? It is not intended for what you're trying to do, so you're trying to use the wrong tool. – Andreas Aug 03 '16 at 19:26
  • I would like to change the URL value with other value, which i know. – RKCY Aug 03 '16 at 19:28
  • Possible duplicate of [Java: String formatting with placeholders](http://stackoverflow.com/questions/17537216/java-string-formatting-with-placeholders) – Jerod Johnson Aug 03 '16 at 19:28
  • How can i split the string. this url is changing the number of the characters always..means dynamic url but " http://localhost/abcdef/servlet/cpd.abcd.build.coupons.CouponValueFormatterServlet?" is always same – RKCY Aug 03 '16 at 19:29
  • I think regex might be desirable here, or, if you know that length of each inner part is always the same you can probably do a substring – SomeStudent Aug 03 '16 at 19:34
  • we can replace with new string from "val= " and ending before "&classGroupid" . Is this possible, if know start and end strings to replace in between data. ? – RKCY Aug 03 '16 at 19:36

1 Answers1

0

If you absolute want to use StringBuilder, you should read the javadoc to find usable methods for your purpose.

That would be:

StringBuilder buf = new StringBuilder("http://localhost/abcdef/servlet/cpd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abcdef&lang=ENG&val=PRCTXT|ABCDE_-1223344&classGroupid=101,201&fgcolor=000000&bgcolor=E0DBD8&width=100&height=80&fontSize=11&fontWeight=normal.");

int start = buf.indexOf("&val=");
if (start != -1) {
    start += 5;
    int end = buf.indexOf("&", start);
    if (end == -1)
        end = buf.length();
    buf.replace(start, end, "123456");
    System.out.println(buf);
}
Andreas
  • 154,647
  • 11
  • 152
  • 247