Im trying to write a routine to replace the '?' in a String with values
supplied by the client.
ie
String myString = "Hello there, my first name is ? and my surname is
?";
String myRegex = "<a regex>";
myString = myString.replaceFirst(myRegex, "David");
myString = myString.replaceFirst(myRegex, "Bevan");
System.out.println(mString);
Required output is "Hello there, my first name is David and my surname
is Bevan"
What do i need as myRegex?
Many thanks
David Bevan
http://www.davidbevan.co.uk
Daniel Dyer - 31 Jan 2006 14:54 GMT
> Im trying to write a routine to replace the '?' in a String with values
> supplied by the client.
If you don't specifically require the markers to be question marks, the
java.text.MessageFormat class might be a better fit for what you are
trying to do.

Signature
Daniel Dyer
http://www.dandyer.co.uk
Arnaud B. - 31 Jan 2006 14:59 GMT
This regex :
"\\?"
should be enough
Regards,
Arnaud
> Im trying to write a routine to replace the '?' in a String with values
> supplied by the client.
[quoted text clipped - 17 lines]
> David Bevan
> http://www.davidbevan.co.uk
Joe Attardi - 31 Jan 2006 23:03 GMT
> Im trying to write a routine to replace the '?' in a String with values
> supplied by the client.
[quoted text clipped - 7 lines]
> myString = myString.replaceFirst(myRegex, "Bevan");
> System.out.println(mString);
You didn't mention if you are able to use Java 5. But, if you are able
to use Java 5 in your project, this can be simplified thusly:
System.out.println(String.format("Hello there, my first name is %s and
my surname is %s", "David", "Bevan"));
Joe Attardi - 01 Feb 2006 00:01 GMT
> Im trying to write a routine to replace the '?' in a String with values
> supplied by the client.
[quoted text clipped - 7 lines]
> myString = myString.replaceFirst(myRegex, "Bevan");
> System.out.println(mString);
You didn't mention if you are able to use Java 5. But, if you are able
to use Java 5 in your project, this can be simplified thusly:
System.out.printf("Hello there, my first name is %s and my surname is
%s", "David", "Bevan");
Or, if you want to store it in a string,
String myString = String.format("Hello there, my first name is %s and
my surname is %s", "David", "Bevan");