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

Introduction

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 );
	input_lower.MakeLower();
	oldone_lower.MakeLower();

	// 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: keim@zeta-software.de.

History

  • 25th January, 2000: Initial post

License

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

Share

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
Offline-Homepage-Baukasten
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralBug PinsussWicket_19-Feb-03 21:53 
GeneralRe: Bug Pinsusswicket_19-Feb-03 21:56 

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 | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 26 Jan 2000
Article Copyright 2000 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid