-2

I'm trying to make a little code in C# that will open a .cfg file and search for a word and then replace it with a text , but that's not everything because i already found how to do this , but the problem is that i want to do some checks like if the file contains "Hello" OR "Hi" OR "blabla" somewhere, replace it with "Bye" (it's just an example)

I want to do this with a simple code

NOTE: already used this one but it looks like it's for one check only ...

string var = File.ReadAllText("mycfg.cfg");
var = var.Replace("Hi", "Bye");
File.WriteAllText("mycfg.cfg", var);

and one more thing is that i'm goin to do the same thing but another 2 times.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Shryder
  • 53
  • 2
  • 11
  • 5
    You can't name a variable `var` as it's a keyword in C#, otherwise your code is ok and should work. – w.b Dec 26 '15 at 22:00
  • 2
    I can't understand what's the issue at all, you want to check some words and replace them, you've already done it, but you want to check it... Please clarify what's you actual goal... – Matías Fidemraizer Dec 26 '15 at 22:00
  • If you are looking at replacing three different character patterns with the same replacement you might want to consider using regular expressions. It may be overkill for what you are doing, but whenever I find myself replacing multiple things with the same text I start to look at regex for a solution. If you are going to be doing multiple replacements of different things you should probably read all the text into a `StringBuilder` (depending on the size of your file) to save on the string reallocation you will be encountering with reach replacement. – pstrjds Dec 26 '15 at 22:03
  • i want to check multiple words and if any of them exists , replace it with "Bye" – Shryder Dec 26 '15 at 22:03
  • @pstrjds i don't understand what u mean , can you write a code that can explain it ^^? – Shryder Dec 26 '15 at 22:04
  • it should be in this way : if( Hi Exists OR Hello Exists OR something Exists){REPLACE THE ONE THAT EXISTS WITH "BYE"} thats it – Shryder Dec 26 '15 at 22:05

3 Answers3

0

I can't understand your problem. You are already replaced one word. Just continue do this with each word which you want to replace. Short syntax.

string fileContent = File.ReadAllText("mycfg.cfg");
fileContent = fileContent.Replace("Hi", "Bye").Replace("Hello", "Bye").Replace("blabla", "Bye");
File.WriteAllText("mycfg.cfg", fileContent);

Full syntax that do actually the same bou don't save intermediate result in the variable.

string fileContent = File.ReadAllText("mycfg.cfg");
fileContent = fileContent.Replace("Hi", "Bye");
fileContent = fileContent.Replace("Hello", "Bye");
fileContent = fileContent.Replace("blabla", "Bye");
File.WriteAllText("mycfg.cfg", fileContent);

StringBuilder can be used in the same way but it'll be more effective only for some cases with long-length strings in input data. If you are not sure or have no performance issues it is better to use String.

Community
  • 1
  • 1
Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
  • One note between this approach and a regular expression is that you cannot call `Replace` in a case insensitive way. If you need it to be case insensitive than you would have to take another approach. – pstrjds Dec 26 '15 at 22:41
0

I want to check multiple words and if any of them exists , replace it with "Bye"

You can first split the input string into words, then use LINQ's Select and finally String.Join:

List<string> wordsToReplace = new List<string> {"Hi", "Hello", "Blabla"};
string file = File.ReadAllText("mycfg.cfg");

string result = String.Join(" ", file.Split().Select(word => wordsToReplace.Contains(word) ? "Bye" : word));

File.WriteAllText("mycfg.cfg", result);

Edit:

var lines = File.ReadLines("mycfg.cfg");

var modified = lines.Select(line => Regex.Replace(line, @"\bplayer\s\d\b", "Bye"));

File.WriteAllLines("mycfg.cfg", modified);
w.b
  • 11,026
  • 5
  • 30
  • 49
  • it looks like i didn't worked , here is my code `List wordsToReplace = new List { "player \"1\"", "player \"2\"", "player \"3\"" }; string file = File.ReadAllText("players/config.cfg"); string result = String.Join(" ", file.Split().Select(word => wordsToReplace.Contains(word) ? "player \"0\"" : word)); File.WriteAllText("players/config.cfg", result);` and btw , ur code made my file without breaks so everything is wrote in one line as it should be like 100 line or 80-70... – Shryder Dec 26 '15 at 22:23
  • It doesn't work because "player 1" consists of 2 words, not 1. – w.b Dec 26 '15 at 22:25
  • so how can i make it work? because this is just what i need ^^ – Shryder Dec 26 '15 at 22:32
  • I'd use `File.ReadLines` instead of `File.ReadAllText`, and then `Regex.Replace` for replacing – w.b Dec 26 '15 at 22:33
  • but i think that `File.ReadLines` reads a specific line right? in my situation the line can be anywhere... :/ – Shryder Dec 26 '15 at 22:36
  • `File.ReadLines` reads the whole file but returns `IEnumerable` instead of 1 string, check https://msdn.microsoft.com/en-us/library/dd383503(v=vs.110).aspx – w.b Dec 26 '15 at 22:38
0

You can do this using a regular expression (you may need to adjust the expression depending on what words and patterns you are wanting to replace. This one should work for what you have described in your question).

var reg = new Regex(@"\b(hi|hello|blabla)", RegexOptions.IgnoreCase);
var fileText = File.ReadAllText("mycfg.cfg");
fileText = reg.Replace("bye");
File.WriteAllText("mycfg.cfg", fileText);

If you want to replace other strings too than you have to adjust the pattern to match what you need to match. So if you need to match Hi, Hello, blabla and player "0" or player "1", adjust your expression.

var reg = new Regex(@"\b(player ""\d""|Hi|Hello|blabla)", RegexOptions.IgnoreCase);
var fileText = File.ReadAllText("mycfg.cfg");
fileText = reg.Replace("bye");
File.WriteAllText("mycfg.cfg", fileText);
pstrjds
  • 16,840
  • 6
  • 52
  • 61
  • I don't think that regex is a good way for reasons of performance. – Vadim Martynov Dec 26 '15 at 22:12
  • @VadimMartynov - Compared to reallocation of the entire file contents four times? Seems it might be similar. I haven't tested it so I can't say for sure. If the regex is too heavy you could go with a StringBuilder to avoid the reallocation. – pstrjds Dec 26 '15 at 22:14
  • Yes, [StringBuilder will be a better way](http://stackoverflow.com/questions/1321331/replace-multiple-string-elements-in-c-sharp) for random big texts. – Vadim Martynov Dec 26 '15 at 22:19
  • @VadimMartynov - In doing a simple test, the regex solution does run slightly slower than the multiple string allocation, but if you start getting into replacing more than three strings it would probably even out. – pstrjds Dec 26 '15 at 22:22
  • is it goin to work with 2 words ? like it should find (player "0") or ... and replace it with something – Shryder Dec 26 '15 at 22:37
  • @user3474108 - It would, you just have to change the expression to match what you are trying to replace. If it is a pattern like `player "0"` and `player "1"` than you could change the expression to `"player \"\d\""` – pstrjds Dec 26 '15 at 22:42
  • it looks like it doesn't work: `var reg = new Regex(@"\b(player \"\d0\"|hello|blabla)", RegexOptions.IgnoreCase);` it says ; expected – Shryder Dec 26 '15 at 22:54
  • @user3474108 - You wrote `\d0` which will match any digit followed by a 0. That would match if your player was `player "10"`. See [Regex Syntax](https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx) for how to build a regular expression to match whatever you need to match. – pstrjds Dec 26 '15 at 23:01