在C#中,如何将字符串包装在方括号中?

时间:2018-07-03 06:01:51

标签: c# string overwrite

例如,以字符串

.Remove "S"

在C#中,我想对字符串应用一个函数,将"S"部分用括号括起来,得到以下字符串:

.Remove ("S")

我找不到该函数,但是我希望编写如下内容:

(".Remove \"S\"").Overwrite("\"S\"", "(\"S\")");

请问有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

你快到了。

这可以通过编程方式实现,如下所示:

string source = ".Remove \"S\"";

// Replace all occurrences of one char with another.

string replacement = source.Replace('\"S\"', '(\"S\")');

有关更多详细信息,请参考-https://docs.microsoft.com/en-us/dotnet/csharp/how-to/modify-string-contents

注意:在Visual Studio界面中,还可以搜索和替换(CTRL+H)