Click here to Skip to main content
15,881,380 members
Home / Discussions / C#
   

C#

 
GeneralRe: Is try - catch block advisable? Pin
OriginalGriff24-Aug-13 2:43
mveOriginalGriff24-Aug-13 2:43 
GeneralRe: Is try - catch block advisable? Pin
Eddy Vluggen24-Aug-13 2:54
professionalEddy Vluggen24-Aug-13 2:54 
GeneralRe: Is try - catch block advisable? Pin
jschell26-Aug-13 8:53
jschell26-Aug-13 8:53 
AnswerRe: Is try - catch block advisable? Pin
Brian Holsen25-Aug-13 18:18
Brian Holsen25-Aug-13 18:18 
AnswerRe: Is try - catch block advisable? Pin
V.26-Aug-13 3:11
professionalV.26-Aug-13 3:11 
QuestionGet User Privileges in a machine for a specific user Pin
Rijz22-Aug-13 20:10
Rijz22-Aug-13 20:10 
AnswerRe: Get User Privileges in a machine for a specific user Pin
Richard MacCutchan22-Aug-13 21:00
mveRichard MacCutchan22-Aug-13 21:00 
QuestionIndexOf Question Pin
AnalogNerd22-Aug-13 5:16
AnalogNerd22-Aug-13 5:16 
I just ran into a real headscratcher. I have code that does the following:

C#
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:
C#
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.

modified 22-Aug-13 11:35am.

AnswerRe: IndexOf Question Pin
fjdiewornncalwe22-Aug-13 5:25
professionalfjdiewornncalwe22-Aug-13 5:25 
GeneralRe: IndexOf Question Pin
AnalogNerd22-Aug-13 5:33
AnalogNerd22-Aug-13 5:33 
AnswerRe: IndexOf Question PinPopular
PIEBALDconsult22-Aug-13 5:49
mvePIEBALDconsult22-Aug-13 5:49 
GeneralRe: IndexOf Question Pin
AnalogNerd22-Aug-13 5:53
AnalogNerd22-Aug-13 5:53 
QuestionDataSet parse date "dd/mm/yyyy" Pin
juliogyn22-Aug-13 5:09
juliogyn22-Aug-13 5:09 
AnswerRe: DataSet parse date "dd/mm/yyyy" Pin
Eddy Vluggen22-Aug-13 6:08
professionalEddy Vluggen22-Aug-13 6:08 
GeneralRe: DataSet parse date "dd/mm/yyyy" Pin
juliogyn22-Aug-13 9:52
juliogyn22-Aug-13 9:52 
AnswerRe: DataSet parse date "dd/mm/yyyy" Pin
fjdiewornncalwe22-Aug-13 10:17
professionalfjdiewornncalwe22-Aug-13 10:17 
GeneralRe: DataSet parse date "dd/mm/yyyy" Pin
juliogyn22-Aug-13 12:25
juliogyn22-Aug-13 12:25 
AnswerRe: DataSet parse date "dd/mm/yyyy" Pin
Forbiddenx23-Aug-13 2:21
Forbiddenx23-Aug-13 2:21 
GeneralRe: DataSet parse date "dd/mm/yyyy" Pin
juliogyn26-Aug-13 2:28
juliogyn26-Aug-13 2:28 
GeneralWindows Forms or WPF? Pin
N8tiv21-Aug-13 22:27
N8tiv21-Aug-13 22:27 
GeneralRe: Windows Forms or WPF? Pin
Richard Andrew x6421-Aug-13 23:22
professionalRichard Andrew x6421-Aug-13 23:22 
GeneralRe: Windows Forms or WPF? Pin
Keith Barrow21-Aug-13 23:30
professionalKeith Barrow21-Aug-13 23:30 
GeneralRe: Windows Forms or WPF? Pin
Mycroft Holmes22-Aug-13 0:47
professionalMycroft Holmes22-Aug-13 0:47 
GeneralRe: Windows Forms or WPF? Pin
Pete O'Hanlon22-Aug-13 0:49
mvePete O'Hanlon22-Aug-13 0:49 
GeneralRe: Windows Forms or WPF? Pin
Dave Kreskowiak22-Aug-13 1:50
mveDave Kreskowiak22-Aug-13 1:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.