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

Case-Insensitive String Replace

By , 25 Jan 2000
 

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)

About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Member
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
Some random fun stuff in German

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralEndlich !!!memberplattenbomber15 May '10 - 1:36 
GeneralRe: Endlich !!!mvpUwe Keim15 May '10 - 2:09 
GeneralMy vote of 1memberHoria Tudosie24 Nov '09 - 4:36 
GeneralA more simple alternative...memberskjpatel30 May '08 - 4:37 
Questionwhy this code not work in UNICODE,_UNICODE setting?memberPankaj.Jain13 Jun '07 - 21:46 
hi,
 

for one of my application, i used this code.but when i run this code without UNICODE,_UNICODE setting this function work fine but when i change setiing to UNICODE this function does not take overall string as there arguments.it only take only one character. can you help me on that.
 
how can i run this code on UNICODE setting.
 
Plz help me.
 
Thanks
 
Pankaj Jain

GeneralMore Efficient Versionmemberpwensing28 Apr '06 - 8:49 
GeneralThanks for the codememberCode4Food15 Nov '05 - 17:38 
QuestionIncludes/Namespaces?memberDarth_Sulfur5 Sep '05 - 14:28 
AnswerRe: Includes/Namespaces?memberChristian Graus5 Sep '05 - 14:40 
GeneralRe: Includes/Namespaces?memberDarth_Sulfur6 Sep '05 - 10:09 
GeneralRe: Includes/Namespaces?memberChristian Graus6 Sep '05 - 11:08 
GeneralRe: Includes/Namespaces?memberDarth_Sulfur7 Sep '05 - 11:27 
GeneralRe: Includes/Namespaces?memberChristian Graus7 Sep '05 - 12:47 
GeneralRe: Includes/Namespaces?memberDarth_Sulfur8 Sep '05 - 13:22 
GeneralRe: Includes/Namespaces?memberChristian Graus8 Sep '05 - 13:27 
QuestionBug correction?memberpetermcwerner28 Jul '05 - 1:35 
GeneralBugsussPeter Werner27 Jul '05 - 8:18 
Generalwhy MakeLower() can not workmemberDavid 200817 Mar '04 - 6:30 
GeneralBugsussWicket_19 Feb '03 - 20:53 
GeneralRe: Bugsusswicket_19 Feb '03 - 20:56 
Generala little optimizationmemberKarstenK15 Apr '02 - 21:17 
GeneralRe: a little optimizationsitebuilderUwe Keim15 Apr '02 - 21:23 
GeneralGood but a little bit slowmemberBraulio Díez22 Nov '01 - 22:33 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 26 Jan 2000
Article Copyright 2000 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid