为什么带有自定义分隔符的 StringTemplates 不起作用?

发布于 2024-12-18 09:16:24 字数 422 浏览 6 评论 0原文

我正在尝试使用带有自定义分隔符的字符串模板。

此代码片段:

new ST("Hello @what@", '@', '@').add("what", "world").render()

抛出org.stringtemplate.v4.compiler.STException,所以我显然做错了。

如果我将其更改为 new ST("Hello $what$", '$', '$'),它就会起作用。为什么@不起作用?

编辑 - 除了 $@ 之外,我尝试了其他字符,到目前为止,除了 $ 之外什么都不起作用。这是怎么回事?

I'm trying to use String Template with custom delimiters.

This code snippet:

new ST("Hello @what@", '@', '@').add("what", "world").render()

throws org.stringtemplate.v4.compiler.STException, so I'm obviously doing it wrong.

If I change it to new ST("Hello $what$", '$', '$'), it works. Why is @ not working?

Edit - I tried other characters besides $ and @, and nothing but $ works so far. What's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在我安 2024-12-25 09:16:24

'$'、'<' 等字符对于 StringTemplate 来说是特殊的,需要转义。

您可以创建一个转义实用程序,例如:

ST st = new ST(StringUtility.escapeStringTemplate(str));

//utility function

public static String escapeStringTemplate(String str){
    String result = str;
    if (result.contains("<")) {
        result = result.replace("<", "\\<");
    }
    if (result.contains("$")) {
        result = result.replace("$", "\\$");
    }

    return result;
}

Characters like '$', '<' are special for StringTemplate and need to be escaped.

You can make a escape util like:

ST st = new ST(StringUtility.escapeStringTemplate(str));

//utility function

public static String escapeStringTemplate(String str){
    String result = str;
    if (result.contains("<")) {
        result = result.replace("<", "\\<");
    }
    if (result.contains("$")) {
        result = result.replace("$", "\\$");
    }

    return result;
}
┼── 2024-12-25 09:16:24

我相信他们一定是不同的角色;开始/停止。
特尔

I believe they have to be different characters; start/stop.
Ter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文