IndexOf Question
-
I just ran into a real headscratcher. I have code that does the following:
var textReader = new StreamReader(path + "\\\\web.config"); var fileContents = textReader.ReadToEnd(); if (fileContents.IndexOf(oldPwd, StringComparison.Ordinal) == -1) { Log("Old Password Not Found.", 0); } else { Log("Replacing old password", 1); fileContents = fileContents.Replace(oldPwd, newPwd); }
What I've found is that if the
oldPwd
contains ^ then things get weird. 1. If the old password ends in a ^, e.g. 123^ then the IndexOf works, but the Replace does will keep the ^ intact. So, with oldPwd = 123^ and newPwd = 456 then Replace will leave the file looking like 456^ 2. If the old password has a ^ in the middle of it then IndexOf will return a -1. I'm baffled to be honest and not sure if it has to do with how I'm reading the file in or what. But I tried this on an online compiler and it worked just fine. So it has to be me, right? EDIT: I also tried it in my code with hard coded strings and it behaves just fine:var mystring = "this is a password 123^ yeah"; Console.WriteLine("Did it find it: " + mystring.IndexOf("123^", StringComparison.Ordinal).ToString()); Console.WriteLine("Did it replace it: " + mystring.Replace("123^", "456")); mystring = "this is a password 123^456 yeah"; Console.WriteLine("Did it find it: " + mystring.IndexOf("123^456", StringComparison.Ordinal).ToString()); Console.WriteLine("Did it replace it: " + mystring.Replace("123^456", "abcdefg"));
FURTHER EDIT: Apparently if I run this through Visual Studio and step through the code it works fine. When I compile the code and run it from the command prompt it exhibits the behavior I outlined above.
-
I just ran into a real headscratcher. I have code that does the following:
var textReader = new StreamReader(path + "\\\\web.config"); var fileContents = textReader.ReadToEnd(); if (fileContents.IndexOf(oldPwd, StringComparison.Ordinal) == -1) { Log("Old Password Not Found.", 0); } else { Log("Replacing old password", 1); fileContents = fileContents.Replace(oldPwd, newPwd); }
What I've found is that if the
oldPwd
contains ^ then things get weird. 1. If the old password ends in a ^, e.g. 123^ then the IndexOf works, but the Replace does will keep the ^ intact. So, with oldPwd = 123^ and newPwd = 456 then Replace will leave the file looking like 456^ 2. If the old password has a ^ in the middle of it then IndexOf will return a -1. I'm baffled to be honest and not sure if it has to do with how I'm reading the file in or what. But I tried this on an online compiler and it worked just fine. So it has to be me, right? EDIT: I also tried it in my code with hard coded strings and it behaves just fine:var mystring = "this is a password 123^ yeah"; Console.WriteLine("Did it find it: " + mystring.IndexOf("123^", StringComparison.Ordinal).ToString()); Console.WriteLine("Did it replace it: " + mystring.Replace("123^", "456")); mystring = "this is a password 123^456 yeah"; Console.WriteLine("Did it find it: " + mystring.IndexOf("123^456", StringComparison.Ordinal).ToString()); Console.WriteLine("Did it replace it: " + mystring.Replace("123^456", "abcdefg"));
FURTHER EDIT: Apparently if I run this through Visual Studio and step through the code it works fine. When I compile the code and run it from the command prompt it exhibits the behavior I outlined above.
The web.config file is just an xml file, so perhaps the way to deal with this is to load the file as an XMLDocument and then simply navigate to the node containing the old password and change its value.
I wasn't, now I am, then I won't be anymore.
-
The web.config file is just an xml file, so perhaps the way to deal with this is to load the file as an XMLDocument and then simply navigate to the node containing the old password and change its value.
I wasn't, now I am, then I won't be anymore.
Unfortunately I don't always know which node the password will be in which is why I went with treating it as a string.
-
I just ran into a real headscratcher. I have code that does the following:
var textReader = new StreamReader(path + "\\\\web.config"); var fileContents = textReader.ReadToEnd(); if (fileContents.IndexOf(oldPwd, StringComparison.Ordinal) == -1) { Log("Old Password Not Found.", 0); } else { Log("Replacing old password", 1); fileContents = fileContents.Replace(oldPwd, newPwd); }
What I've found is that if the
oldPwd
contains ^ then things get weird. 1. If the old password ends in a ^, e.g. 123^ then the IndexOf works, but the Replace does will keep the ^ intact. So, with oldPwd = 123^ and newPwd = 456 then Replace will leave the file looking like 456^ 2. If the old password has a ^ in the middle of it then IndexOf will return a -1. I'm baffled to be honest and not sure if it has to do with how I'm reading the file in or what. But I tried this on an online compiler and it worked just fine. So it has to be me, right? EDIT: I also tried it in my code with hard coded strings and it behaves just fine:var mystring = "this is a password 123^ yeah"; Console.WriteLine("Did it find it: " + mystring.IndexOf("123^", StringComparison.Ordinal).ToString()); Console.WriteLine("Did it replace it: " + mystring.Replace("123^", "456")); mystring = "this is a password 123^456 yeah"; Console.WriteLine("Did it find it: " + mystring.IndexOf("123^456", StringComparison.Ordinal).ToString()); Console.WriteLine("Did it replace it: " + mystring.Replace("123^456", "abcdefg"));
FURTHER EDIT: Apparently if I run this through Visual Studio and step through the code it works fine. When I compile the code and run it from the command prompt it exhibits the behavior I outlined above.
This confused me a while back as well.
^
is the escape character on the command line, so escape it (^^
) and it should work as expected. -
This confused me a while back as well.
^
is the escape character on the command line, so escape it (^^
) and it should work as expected.I owe you a beer! Thank you!