Click here to Skip to main content
15,119,410 members
Articles / Desktop Programming / MFC
Posted 25 Jan 2000


14 bookmarked

Case-Insensitive String Replace

Rate me:
Please Sign up or sign in to vote.
3.47/5 (10 votes)
25 Jan 2000CPOL
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:

Deutschsprachiges Forum zu Elektromobilität und Tesla  
Windows 10 Ereignisanzeige  
German Developer Community  
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

QuestionAvoid bugs by doing two passes and making substitutions in reverse order Pin
ckorda14-Dec-18 17:05
Memberckorda14-Dec-18 17:05 
GeneralEndlich !!! Pin
plattenbomber15-May-10 2:36
Memberplattenbomber15-May-10 2:36 
GeneralRe: Endlich !!! Pin
Uwe Keim15-May-10 3:09
sitebuilderUwe Keim15-May-10 3:09 
GeneralMy vote of 1 Pin
Horia Tudosie24-Nov-09 5:36
MemberHoria Tudosie24-Nov-09 5:36 
GeneralA more simple alternative... Pin
skjpatel30-May-08 5:37
Memberskjpatel30-May-08 5:37 
Questionwhy this code not work in UNICODE,_UNICODE setting? Pin
Pankaj.Jain13-Jun-07 22:46
professionalPankaj.Jain13-Jun-07 22:46 
GeneralMore Efficient Version Pin
pwensing28-Apr-06 9:49
Memberpwensing28-Apr-06 9:49 
GeneralThanks for the code Pin
Code4Food15-Nov-05 18:38
MemberCode4Food15-Nov-05 18:38 
QuestionIncludes/Namespaces? Pin
Lord Kixdemp5-Sep-05 15:28
MemberLord Kixdemp5-Sep-05 15:28 
AnswerRe: Includes/Namespaces? Pin
Christian Graus5-Sep-05 15:40
protectorChristian Graus5-Sep-05 15:40 
GeneralRe: Includes/Namespaces? Pin
Lord Kixdemp6-Sep-05 11:09
MemberLord Kixdemp6-Sep-05 11:09 
GeneralRe: Includes/Namespaces? Pin
Christian Graus6-Sep-05 12:08
protectorChristian Graus6-Sep-05 12:08 
GeneralRe: Includes/Namespaces? Pin
Lord Kixdemp7-Sep-05 12:27
MemberLord Kixdemp7-Sep-05 12:27 
GeneralRe: Includes/Namespaces? Pin
Christian Graus7-Sep-05 13:47
protectorChristian Graus7-Sep-05 13:47 
GeneralRe: Includes/Namespaces? Pin
Lord Kixdemp8-Sep-05 14:22
MemberLord Kixdemp8-Sep-05 14:22 
GeneralRe: Includes/Namespaces? Pin
Christian Graus8-Sep-05 14:27
protectorChristian Graus8-Sep-05 14:27 
QuestionBug correction? Pin
petermcwerner28-Jul-05 2:35
Memberpetermcwerner28-Jul-05 2:35 
AnswerRe: Bug correction? Pin
AberAber26-Nov-13 19:22
MemberAberAber26-Nov-13 19:22 
GeneralBug Pin
petermcwerner27-Jul-05 9:18
Memberpetermcwerner27-Jul-05 9:18 
Generalwhy MakeLower() can not work Pin
David 200817-Mar-04 7:30
MemberDavid 200817-Mar-04 7:30 
GeneralBug Pin
Wicket_19-Feb-03 21:53
sussWicket_19-Feb-03 21:53 
GeneralRe: Bug Pin
wicket_19-Feb-03 21:56
susswicket_19-Feb-03 21:56 
Generala little optimization Pin
KarstenK15-Apr-02 22:17
mveKarstenK15-Apr-02 22:17 
GeneralRe: a little optimization Pin
Uwe Keim15-Apr-02 22:23
sitebuilderUwe Keim15-Apr-02 22:23 
GeneralGood but a little bit slow Pin
Braulio Dez22-Nov-01 23:33
MemberBraulio Dez22-Nov-01 23:33 

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.