string replace

alidoran

New member
Joined
Jul 16, 2018
Messages
1
Programming Experience
10+
[FONT=&quot]hello[/FONT]
[FONT=&quot]i want to edit a file by c# string by this code[/FONT]
[FONT=&quot] string text = File.ReadAllText(@"c:\a.txt", Encoding.UTF8);
string text1 = text.Replace("xx", XX");
File.WriteAllText(@"d:\a.txt", text1);[/FONT]

[FONT=&quot]i want to search in string and when find example replace exam\ple and 4 word after exam\ple with exam\plexxxx[/FONT]
[FONT=&quot]problem is i dont know what is the 4 word and i want replace.[/FONT]
[FONT=&quot]i want any like * in windows seach.[/FONT]
[FONT=&quot]in windows search * can support all character.[/FONT]
 
You can use wildcards in C# in certain circumstances but not using String.Replace. One option in your case might be to use a regular expression. I've never got around to getting proficient with them so I can't tell you exactly what expression to use but the Regex.Replace method lets you specify a pattern to replace rather than specific text and that pattern can be all kinds of complex.

Another, more manual, option would be to use String.IndexOf in a loop. You could first use IndexOf to find "exam\ple" and then use it in a loop to the find the next four words. Once you know where the fourth word ends, you can effectively replace that part of the String, probably by appending to a StringBuilder. I'd suggest playing around with that, building up from a simple example, i.e. just replace the one word, then two, etc.

One thing that you should probably consider is what to do if there's punctuation within those subsequent four words, if that can happen.
 
Back
Top Bottom