C#中的字符串或宏

siv*_*iva 2 c#

我的应用程序中有超过90个文件(.cs).

我用了一个声明

string str = MyMessages.IDS_STR_STRING1; 
MyMessages.IDS_STR_STRING2; 
MyMessages.IDS_STR_STRING3; 
Run Code Online (Sandbox Code Playgroud)

在许多文件中几乎说了40多个文件.

MyMessages是静态类.

我现在在这个类中添加了另一个函数,例如GetMyString(字符串标识符).所以现在上面的statmenet将改为

MyMessages.GetMyString("IDS_STR_STRING1"); 
MyMessages.GetMyString("IDS_STR_STRING2"); 
MyMessages.GetMyString("IDS_STR_STRING3"); 
Run Code Online (Sandbox Code Playgroud)

等等....

现在搜索和替换每个语句很繁琐,可能导致手动错误.我可以编写任何宏/工具来找到字符串并以适当的格式替换吗?

Jan*_*oom 5

您可以在Visual Studio中使用支持正则表达式的替换功能.

就像是:

输入

// search for a whitespace character, then the string 'MyMessages.'
//  match the next group of characters that is a word (a-zA-Z) or an _
//  and capture it into group number 1
\sMyMessages\.([\w_]+);
Run Code Online (Sandbox Code Playgroud)

更换

// Replace it with 'MyMessages.GetMyString("', then insert group 1
// then add '");' to the string.
MyMessages.GetMyString("\1");
Run Code Online (Sandbox Code Playgroud)