Wednesday, March 11, 2009

got the macro replacement blues

As crazy as it may seem, I have implemented this type of silly macro replacement stuff about a million times:

public String replace( String message, Object ... argz ) {
int i = 0;
for ( Object arg : argz ) {
message = message.replaceAll(
"\\{" + ( i++ ) + "}"
, arg.toString()
);
}
return message;
}


Basicly it converts something like:

"this is some {0}, Mr. {1}... some real {0}" + "mess" and "Man"

to something like:

this is some mess, Mr. Man... some real mess

The only "tricky" part is that you have to escape the opening curly with a backslash and then escape the backslash with a backslash...

Sigh...

No comments:

Post a Comment