#strings 和标准 String 方法之间的区别 [英] Difference between #strings and standard String methods
问题描述
有什么区别:
${obj.title.replaceAll(' ', '-')
和百里香专用:
${#strings.replace(obj.title, ' ', '-')}
我知道在第一个示例中我使用标准 String 方法,在第二个示例中我使用 Thymeleaf 中的 Strings 类.哪一个是首选,为什么?当 String 类中的方法无法使用时,是否有任何特定情况应将其替换为等效的 Strings?
I know that in first example I use standard String method and in second one I use the Strings class from Thymeleaf. Which one is preferred and why? Is there any specific case when methods from String class cannot be used and they should be replaced with Strings equivalent?
推荐答案
以下是一些不同之处:
strings.replace
第一个参数不需要是字符串,可以为空strings.replace
第二个参数,您正在搜索的参数,不是正则表达式,而 sdkString#replaceAll
将与正则表达式一起使用
strings.replace
first param doesn't need to be string and can be nullstrings.replace
second param, the one you're searching for, is not a regex while the sdkString#replaceAll
will work with regex
您可以找到百里香叶似乎更可取的情况,例如.当您不想使用空检查或 toString 调用弄脏代码时.话虽如此,我将始终使用 SDK API,因为
You can find cases when the Thymeleaf seems preferable, for eg. when you don't want to dirty the code with null checks or toString calls. That being said I would always use the SDK API because
- 它的知名度和文档要高得多,因此新开发人员更容易理解
- Java api 将始终以向后兼容的方式更新;Tymeleaf api 可以在未来更改实现细节,并且由于社区小得多,您不确定他们是否会专注于向后兼容性.任何不得不在项目中升级第三方库的人都知道我在说什么.如果有一天他们决定使用正则表达式重新实现替换方法,那么您将不得不更改代码以转义正则表达式特殊字符.
这篇关于#strings 和标准 String 方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!