Thursday, March 22, 2012

Strange issue with Replace() on a stringbuider

I am having a problem with a search/replace function I wrote. It doesnt seem to match anything if it contains an upper case char. Basically, if what I am trying to replace contains an upper case character, then the Replace() function will not find it. It will find all identical instances with lower case chars, tho.

source : abcdeABCDE
replace : d,! -or- D!
result : abc!eABCDE

oReplace is a StringDictionary,
textboxes are all RichTextBoxen

I have tried with strings, stringbuilders, adding differenty ways, calling .ToString() on everyhing, adding to the SB as char array (.ToCharArray)!


for (i = 0; i <= iLines; i++) {
sLine = this.txtReplace.Lines[i].ToString();
arrLine = sLine.Split(new char[]{','},2);
if (arrLine.GetUpperBound(0) != 1) {
this.txtResult.Text = "Bad replace line detected : " + sLine;
return;
} else {
if (oReplace.ContainsKey(arrLine[0])) {
this.txtResult.Text = "duplicate replace specified : " + arrLine[0];
return;
} else {
oReplace.Add(arrLine[0].ToString(),arrLine[1].ToString());
}
}
}
StringBuilder sb = new StringBuilder();

sb.Append(this.txtSource.Text.ToString());
foreach (string sOrig in oReplace.Keys) {
sOld = sOrig.ToString();
sNew = oReplace[sOld].ToString();
this.txtResult.Text += "[" + sOrig + "][" + sNew + "]" + System.Environment.NewLine;
sb = sb.Replace(sOld.ToString(),sNew.ToString());
}

this.txtResult.Text += sb.ToString();

Can you give the exact contents of the dictionary (keys and values)?

0 comments:

Post a Comment