This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Insertion strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Insertion strings" Watch "Insertion strings" New topic
Author

Insertion strings

Dave Jones
Ranch Hand

Joined: Feb 20, 2005
Posts: 77
Hello ranchers !
I have a method that gets a target string and an array of strings.
I need to insert the strings from the array into the target string.
the target string has brackets {1} with numbers, which means that instead of each bracket comes a string from the array(of the index in the brackets)
Example:

Target string: "we can finally {0} home and watch the {2} on our big {1} "
Array: { "stay","T.V", "super bowl"}

Final result:
"we can finally stay home and watch the super bowl on our big T.V "

Now this is already done in some applications like Logger.
But I can't use the Loggers' methods since they are private and use private members.
So, does anyone know an existing class/methods that can do the job for me?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Instead of an existing class, you could try to make one yourself.
Go from 0, up to the size of the array, and replace all occurences of the {x} pattern by the item in the array.


[My Blog]
All roads lead to JavaRanch
Dave Jones
Ranch Hand

Joined: Feb 20, 2005
Posts: 77
How do I easily search for the pattern {x} in a String ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You could use String.indexOf to find out where the pattern is, and loop until there are none left.
An easier approach would be to use String.replaceAll using a regular expression.
Have a look at the API for more.
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
Try using the Matcher and Pattern classes in java.util.regex

Since I knew they were available, but hadn't really looked at them yet, I decided to do what you were trying to do. Here's what I came up with.



A couple things to point out, since I found them rather strange. Yes, the "\\{" is correct. The reason for this is because the bytecode compiler might interpret it differently if only one backslash was used (That was the biggest problem. Solution found in the javadocs). Also, neither Matcher nor Pattern has any constructors to use, so you have to compile a Pattern with a String and get a matcher from the Pattern object you just made with any CharSequence object.

Last thing, I just put replaceFirst, but replaceAll is also available in case you have multiple matches for a pattern.

Good Luck.
[ January 26, 2006: Message edited by: Craig Tyler ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Another one:
Dave Jones
Ranch Hand

Joined: Feb 20, 2005
Posts: 77
Thank you all very very much !
You are truly the greatest!
And saved me so much time, I was struggling with the 'replace' method and now I figured it out thanks to you.
Have a nice day !
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
One last go. I swear I should look at the API more often. After seeing the replaceAll method in the String class, I was looking around, so here you go.



Regular expressions are a bit of an overkill for something simple like this. Plus you don't have to fiddle with extra backslashes.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Craig,
First, your code does not compile (at least uner 1.4)
replace method does not allow strings. Perhaps you wanted to write replaceAll instead.
Second, are you sure you don't need the backslashes ? The character '{' is a special symbol in regular expressions. If you don't escape it, you'll have funny results
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
The replace() method in the String class is for 1.5. My apologies, I should have mentioned that. Here's the declaration:

String replace(CharSequence target, CharSequence replacement)

I noticed the funny results whenever using either the matcher or the replaceAll without using backslashes on the opening brace, but those both use regular expressions as opposed to an ordinary character sequence. So it works as long as you compile it with 1.5 (or using -source 1.4 on a 1.5 compiler).
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Ok then Thanks for the info.
I'll have to upgrade to 1.5 one of these days
 
jQuery in Action, 2nd edition
 
subject: Insertion strings
 
Similar Threads
constructor ,setter and getter methods with array of strings as a parameter
Can you tell me how to answer such question?
Sun Cirtification
An object to hold several arrays
Go New Yawk