"I spent almost a year of my life working with The Phantom Duo," writes David B via the Submit to TDWTF Extension, "while their tenure resulted in a lot of headaches, a few lost jobs, and a multi-million dollar explosion, there was one positive result: lots and lots of bad code."
"At least, that's a good thing for me, since I haven't worked with the code in years: it's like I have my own, personal The Daily WTF archive on a flash drive. This particular method seemed worth sharing - I even added a few comments to help understand it."
/// <summary> /// Called by MatchWord /// </summary> private bool ChangeWord(ref string str, bool checkCase, bool checkWholeWord, string strFind, string strReplace) { // declare a local that will be returned; it's prefered to use only one "return" statement, at the end bool changed = false; // create copies of some of the parameters; you can never have too many copies string lStr; string lStrFind; // some people use caseSensitive or caseInsensitive... this is more clear if (checkCase) { lStr = str; lStrFind = strFind; } else { // case-insensitive change, convert to UPPERCASE, forget existence of other cultures lStr = str.ToUpper(); lStrFind = strFind.ToUpper(); } // by check whole word, I mean check entire string if (checkWholeWord) { if (lStr == lStrFind) { str = strReplace; changed = true; } } else { // reinvent the wheel int start = 0; int index = 0; while (start < lStr.Length && index >= 0) { index = lStr.IndexOf(lStrFind, start); if (index >= 0) { str = str.Remove(index, strFind.Length); str = str.Insert(index, strReplace); changed = true; // unless strReplace == strFind if (checkCase) lStr = str; else lStr = str.ToUpper(); start = index + strReplace.Length; } } } return changed; }
[Advertisement]
BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!