Java字符串格式&与参数位置进行本地化

发布于 2025-01-31 02:31:30 字数 337 浏览 2 评论 0原文

好吧,例如,您有一个要格式化的字符串,例如 “这是%s和%s的字符串” 如果我将其格式化为“约翰”和“艾比”,这将很好地完成工作。 但是,如果相反,我想键入“艾比”,然后键入“约翰”,我可以做到,它不漂亮,看起来像这样: “这是一根弦,由%2 $ s和%1 $ s”,其次是“约翰”,“ abby”的格式,它会格式化它,但我想要的是很多工作(在大文件,仅在运行时格式化多个变量),以确保一切都正确。我的问题是,有没有办法以“漂亮”的方式使其起作用?例如,C#的格式是用“ {0},{1}”完成的,它比Java好一百万倍。

我尝试用正则替代者替换“ {0}”为“%1 $ s”。如果有人可以解决这个问题,它将对我有所帮助,并将对此表示赞赏。提前致谢

Alright, say you have a string you want to format, for example
"this is a string by %s and %s"
and this will do the job just fine if i format it for example with "John" and "Abby"
but, what if instead, I want to type "Abby" and then "John", I can do it, its just not pretty and will look something like this:
"this is a string by %2$s and %1$s" followed by the formatting of "John", "Abby" and it will format it however I want, but the problem with this is its a lot of work (in large files, that are formatted only in runtime with multiple variables) to make sure everything is correct. my question is this, is there a way to have it work in a "prettier" way? for example C#'s formatting is done with "{0}, {1}" which is a million times better than Java's.

I have tried a replacer with regex to replace "{0}" to, for example, "%1$s". if somebody has a solution to this problem it would help me and will be appreciated. thanks in advance

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文