带参数调用resx

本文关键字:resx 调用 参数 | 更新日期: 2023-09-27 18:08:44

我在我的项目中翻译了resx文件,我想替换所有的文本它将从这个文件变成,当我使用没有参数的文本,就像下面的

objRes.ErrorMsg = Group.err_message_duplicate;//This replace the entire string

问题是我想替换以下字符串

 objRes.ErrorMsg = "user " + username + " doesn't exist in" + table;

对于它,我在resx文件中创建以下条目

User  {0} doesn't exist in {1}

我应该如何使用resx与参数?

带参数调用resx

使用string.Format:

objRes.ErrorMsg = string.Format("User {0} doesn't exist in {1}", username, table);

如果来自变量Group.err_message_duplicate,则使用:

objRes.ErrorMsg = string.Format(Group.err_message_duplicate, username, table);

使用String.Format

objRes.ErrorMsg = string.Format(Group.err_message_duplicate, username, table);

这不是正则表达式。看起来你想要一个简单的字符串格式,如:

objRes.ErrorMsg = string.Format(Resources.MyMessage, username, table);