作者:符小萧_103 | 来源:互联网 | 2023-09-23 18:13
IhaveaservicemethodthattakesaStringandthenreplacestagsintheStringwithitemsfromat
I have a service method that takes a String and then replaces tags in the String with items from a tag library. As follows:
我有一个服务方法,它接受一个String,然后用标签库中的项替换String中的标签。如下:
for( MetaDataDTO tag : tagValues )
{
message = message.replace( tag.getKey(), tag.getText1() );
}
Obviously; this make heaps of new strings and is BAD. But the StringBuilder replace method is cumbersome to use for multiple strings inside one string. How can I make my method more efficient?
明显;这使得大量的新字符串变得糟透了。但StringBuilder替换方法对于一个字符串中的多个字符串使用是很麻烦的。如何使我的方法更有效?
It is for use with blocks of text such as:
它适用于文本块,例如:
Dear #firstName#, your application for #applicationType# has been #approvedRejected# sorry.
亲爱的#firstName#,#applicationType#的申请已被#approvedRejected#sorry。
Where #firstName#, etc are the keys in a meta data database. It is also possible that tags may not be surrounded by hash characters.
其中#firstName#等是元数据数据库中的键。标签也可能不被散列字符包围。
2 个解决方案