• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Insertion strings

 
Dave Jones
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Jones
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I easily search for the pattern {x} in a String ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another one:
 
Dave Jones
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok then Thanks for the info.
I'll have to upgrade to 1.5 one of these days
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic