Click here to Skip to main content
15,498,622 members
Home / Discussions / C#
   

C#

 
GeneralRe: Is try - catch block advisable? Pin
OriginalGriff24-Aug-13 3:43
mveOriginalGriff24-Aug-13 3:43 
GeneralRe: Is try - catch block advisable? Pin
Eddy Vluggen24-Aug-13 3:54
professionalEddy Vluggen24-Aug-13 3:54 
GeneralRe: Is try - catch block advisable? Pin
jschell26-Aug-13 9:53
Memberjschell26-Aug-13 9:53 
AnswerRe: Is try - catch block advisable? Pin
Brian Holsen25-Aug-13 19:18
MemberBrian Holsen25-Aug-13 19:18 
AnswerRe: Is try - catch block advisable? Pin
V.26-Aug-13 4:11
professionalV.26-Aug-13 4:11 
QuestionGet User Privileges in a machine for a specific user Pin
Rijz22-Aug-13 21:10
MemberRijz22-Aug-13 21:10 
AnswerRe: Get User Privileges in a machine for a specific user Pin
Richard MacCutchan22-Aug-13 22:00
mveRichard MacCutchan22-Aug-13 22:00 
QuestionIndexOf Question Pin
AnalogNerd22-Aug-13 6:16
MemberAnalogNerd22-Aug-13 6: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 6:25
professionalfjdiewornncalwe22-Aug-13 6:25 
GeneralRe: IndexOf Question Pin
AnalogNerd22-Aug-13 6:33
MemberAnalogNerd22-Aug-13 6:33 
AnswerRe: IndexOf Question Pin
PIEBALDconsult22-Aug-13 6:49
professionalPIEBALDconsult22-Aug-13 6:49 
GeneralRe: IndexOf Question Pin
AnalogNerd22-Aug-13 6:53
MemberAnalogNerd22-Aug-13 6:53 
QuestionDataSet parse date "dd/mm/yyyy" Pin
juliogyn22-Aug-13 6:09
Memberjuliogyn22-Aug-13 6:09 
AnswerRe: DataSet parse date "dd/mm/yyyy" Pin
Eddy Vluggen22-Aug-13 7:08
professionalEddy Vluggen22-Aug-13 7:08 
GeneralRe: DataSet parse date "dd/mm/yyyy" Pin
juliogyn22-Aug-13 10:52
Memberjuliogyn22-Aug-13 10:52 
AnswerRe: DataSet parse date "dd/mm/yyyy" Pin
fjdiewornncalwe22-Aug-13 11:17
professionalfjdiewornncalwe22-Aug-13 11:17 
GeneralRe: DataSet parse date "dd/mm/yyyy" Pin
juliogyn22-Aug-13 13:25
Memberjuliogyn22-Aug-13 13:25 
AnswerRe: DataSet parse date "dd/mm/yyyy" Pin
Forbiddenx23-Aug-13 3:21
MemberForbiddenx23-Aug-13 3:21 
GeneralRe: DataSet parse date "dd/mm/yyyy" Pin
juliogyn26-Aug-13 3:28
Memberjuliogyn26-Aug-13 3:28 
GeneralWindows Forms or WPF? Pin
N8tiv21-Aug-13 23:27
MemberN8tiv21-Aug-13 23:27 
GeneralRe: Windows Forms or WPF? Pin
Richard Andrew x6422-Aug-13 0:22
professionalRichard Andrew x6422-Aug-13 0:22 
GeneralRe: Windows Forms or WPF? Pin
Keith Barrow22-Aug-13 0:30
professionalKeith Barrow22-Aug-13 0:30 
GeneralRe: Windows Forms or WPF? Pin
Mycroft Holmes22-Aug-13 1:47
professionalMycroft Holmes22-Aug-13 1:47 
GeneralRe: Windows Forms or WPF? Pin
Pete O'Hanlon22-Aug-13 1:49
mvaPete O'Hanlon22-Aug-13 1:49 
GeneralRe: Windows Forms or WPF? Pin
Dave Kreskowiak22-Aug-13 2:50
mveDave Kreskowiak22-Aug-13 2: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.