Click here to Skip to main content
Click here to Skip to main content

Case-Insensitive String Replace

, 25 Jan 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
Function to replace all occurences of a string within another, ignoring the case


This is a simple function that acts like CString::Replace(), except that the case of the string to search for is ignored.

The whole code follows:

// instr:  string to search in.
// oldstr: string to search for, ignoring the case.
// newstr: string replacing the occurrences of oldstr.
CString ReplaceNoCase( LPCTSTR instr, LPCTSTR oldstr, LPCTSTR newstr )
	CString output( instr );

	// lowercase-versions to search in.
	CString input_lower( instr );
	CString oldone_lower( oldstr );

	// search in the lowercase versions,
	// replace in the original-case version.
	int pos=0;
	while ( (pos=input_lower.Find(oldone_lower,pos))!=-1 ) {

		// need for empty "newstr" cases.
		input_lower.Delete( pos, lstrlen(oldstr) );	
		input_lower.Insert( pos, newstr );

		// actually replace.
		output.Delete( pos, lstrlen(oldstr) );
		output.Insert( pos, newstr );

	return output;

The function's implementation is rather simple: it creates several copies of the strings. If you need a memory- and speed- optimized replace function, this one is probably not the best for you. Anyway, in my project, it worked just well.

Please feel free to ask any questions you have by e-mail:


  • 25th January, 2000: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.
In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.
Some cool, free software from us:
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email

Comments and Discussions

GeneralEndlich !!! Pinmemberplattenbomber15-May-10 1:36 
GeneralRe: Endlich !!! PinmvpUwe Keim15-May-10 2:09 
GeneralMy vote of 1 PinmemberHoria Tudosie24-Nov-09 4:36 
GeneralA more simple alternative... Pinmemberskjpatel30-May-08 4:37 
Questionwhy this code not work in UNICODE,_UNICODE setting? PinmemberPankaj.Jain13-Jun-07 21:46 
GeneralMore Efficient Version Pinmemberpwensing28-Apr-06 8:49 
While I was making some slight modifications to this for a project I was working on, I made some modifications that ended up working faster. The basic gist is that instead of performing the replace in the old string and the new string, I keep some extra track of positions so that I only need to do the replace in the main string. There was about a 27% gain in speed with this version compared to the original when I tested it (I ran it on a fairly large string 1000 times for each function). Sorry for the minor differences in parameters and such, this way suited my needs better.
void newReplace2(CString& mainStr, CString& oldStr, CString& newStr)
//Prevent infinite loop when user tries to replace nothing.
if (oldStr != "")
int oldLen = oldStr.GetLength(), newLen = newStr.GetLength();
int nPos = 0, lastPos = 0, newPos = 0, posDiff = 0;
//Copies of the main string and the string to be replaced that
//are made lower case
CString mainStr_low(mainStr);
CString oldStr_low(oldStr);
while ((nPos = mainStr_low.Find(oldStr_low, nPos)) != -1)
//Calc number of characters between last match and
//new match
posDiff = nPos - lastPos;

//Determine the position of the match in the string
//to be returned
newPos += posDiff;
//Do the replace in the main string that will be
mainStr.Delete(newPos, oldLen);
mainStr.Insert(newPos, newStr);
//Skip the rest of the old string
nPos += oldLen;
//Skip the new position to the end of the new string
//To keep it inline with the position in the old
newPos += newLen;
//Needed to determine posDiff above
lastPos = nPos;

GeneralThanks for the code PinmemberCode4Food15-Nov-05 17:38 
QuestionIncludes/Namespaces? PinmemberDarth_Sulfur5-Sep-05 14:28 
AnswerRe: Includes/Namespaces? PinmemberChristian Graus5-Sep-05 14:40 
GeneralRe: Includes/Namespaces? PinmemberDarth_Sulfur6-Sep-05 10:09 
GeneralRe: Includes/Namespaces? PinmemberChristian Graus6-Sep-05 11:08 
GeneralRe: Includes/Namespaces? PinmemberDarth_Sulfur7-Sep-05 11:27 
GeneralRe: Includes/Namespaces? PinmemberChristian Graus7-Sep-05 12:47 
GeneralRe: Includes/Namespaces? PinmemberDarth_Sulfur8-Sep-05 13:22 
GeneralRe: Includes/Namespaces? PinmemberChristian Graus8-Sep-05 13:27 
QuestionBug correction? Pinmemberpetermcwerner28-Jul-05 1:35 
AnswerRe: Bug correction? PinmemberAberAber26-Nov-13 18:22 
GeneralBug PinsussPeter Werner27-Jul-05 8:18 
Generalwhy MakeLower() can not work PinmemberDavid 200817-Mar-04 6:30 
GeneralBug PinsussWicket_19-Feb-03 20:53 
GeneralRe: Bug Pinsusswicket_19-Feb-03 20:56 
Generala little optimization PinmemberKarstenK15-Apr-02 21:17 
GeneralRe: a little optimization PinsitebuilderUwe Keim15-Apr-02 21:23 
GeneralGood but a little bit slow PinmemberBraulio Díez22-Nov-01 22:33 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141029.1 | Last Updated 26 Jan 2000
Article Copyright 2000 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid