Best way to replace tokens in a large text template

后端 未结 10 1741
我在风中等你
我在风中等你 2020-12-08 17:14

I have a large text template which needs tokenized sections replaced by other text. The tokens look something like this: ##USERNAME##. My first instinct is just to use Stri

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 17:25

    Regular expressions would be the quickest solution to code up but if you have many different tokens then it will get slower. If performance is not an issue then use this option.

    A better approach would be to define token, like your "##" that you can scan for in the text. Then select what to replace from a hash table with the text that follows the token as the key.

    If this is part of a build script then nAnt has a great feature for doing this called Filter Chains. The code for that is open source so you could look at how its done for a fast implementation.

提交回复
热议问题