【发布时间】:2019-07-06 13:46:39
【问题描述】:
我想为 ex 找到所有出现的字符。较长的字符串“The Haunting of Hill House”中的“H”。我正在使用此代码,但我仅按特定位置搜索。
static void Main(string[] args)
{
string str = "The Haunting of Hill House!";
Console.WriteLine("String: " + str);
// replacing character at position 7
int pos = 7;
char rep = 'p';
string res = str.Substring(0, pos) + rep + str.Substring(pos + 1);
Console.WriteLine("String after replacing a character: " + res);
Console.ReadLine();
}
【问题讨论】:
-
为什么不能使用 string.Replace() ?
-
@BasilKosovan 因为这就是任务的方式,这就是我必须执行的方式,我已经说过我不应该使用 replace()。
-
@bimixbimix,我更新了我的答案,请查看。
-
@PrasadTelkikar 我想找到所有出现的“H”并用“p”更改所有这些。
-
你可以看看我的回答