/**
* 通过 key获取对应的显示文字
* @param key
* @param args 替换参数
*/
public static getText(key: string, ...args: any): string {
var str = this.textMap[key];
if (!str) {
str = 'Text not found,the key is:' + key;
} else {
str = str.replace(/\{(\d+)\}/g, function (match, index) {
if (args.length > index) {
return args[index];
} else {
return "";
}
});
}
return str;
}
simple:
textMap["test"] = "hellworld {0}, gody bye {1}";
执行 this.getText("test", "Jim", "Sam");
返回的字符串就是"hellworld Jim, gody bye Sam";
字符串替换
最新推荐文章于 2023-03-15 10:45:28 发布