-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.

7
  • Can you use String.split()? Commented Aug 3, 2016 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. Commented Aug 3, 2016 at 19:26
  • I would like to change the URL value with other value, which i know. Commented Aug 3, 2016 at 19:28
  • Possible duplicate of Java: String formatting with placeholders Commented Aug 3, 2016 at 19:28
  • How can i split the string. this url is changing the number of the characters always..means dynamic url but " localhost/abcdef/servlet/…?" is always same Commented Aug 3, 2016 at 19:29

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Andreas. Its working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.