I have a text file called sample.txt and its contents look like
John Howard Carpenter @#@ born in Carthage, New York,
to mother Milton Jean (Carter) and father Howard Ralph Carpenter.
His family moved to Bowling @#@, Kentucky, @#@ his father, a professor,
was @#@ of the music @#@ at Western Kentucky University.
How do I replace all @#@ by the numbers starting from 1, then incremented by +1 till no @#@ is left in the file.
I've tried
- string x=File.ReadAllText(@"C:sample.txt");
- string y="@#@";
- int i=1;
- MatchCollection matches = Regex.Matches(x, y, RegexOptions.IgnoreCase);
- if (matches.Count>0)
- {
- foreach (Match m in matches) {
- string n=x.Replace(y,i.ToString());
- i++;
- File.WriteAllText(y,n);
- }
- }
- MessageBox.Show("Done");
It does not do anything
BTW: The expected output is
John Howard Carpenter 1 born in Carthage, New York, to mother Milton Jean (Carter) and father Howard Ralph Carpenter. His family moved to Bowling 2, Kentucky, 3 his father, a professor, was 4 of the music 5 at Western Kentucky University.