Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Regex for finding a '?' in a String

Thread view: 
junk1@davidbevan.co.uk - 31 Jan 2006 14:50 GMT
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");


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.