Click here to Skip to main content
Licence CPOL
First Posted 25 Jan 2000
Views 114,692
Bookmarked 12 times

Case-Insensitive String Replace

By | 25 Jan 2000 | Article
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)

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 is also teached programming to students at the local university.
 
In his free time, he does climbing, running and mountain biking. You can watch him most of the day (and probably night) programming.
 
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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralEndlich !!! Pinmemberplattenbomber1:36 15 May '10  
GeneralRe: Endlich !!! PinmvpUwe Keim2:09 15 May '10  
GeneralMy vote of 1 PinmemberHoria Tudosie4:36 24 Nov '09  
GeneralA more simple alternative... Pinmemberskjpatel4:37 30 May '08  
Questionwhy this code not work in UNICODE,_UNICODE setting? PinmemberPankaj.Jain21:46 13 Jun '07  
GeneralMore Efficient Version Pinmemberpwensing8:49 28 Apr '06  
GeneralThanks for the code PinmemberCode4Food17:38 15 Nov '05  
QuestionIncludes/Namespaces? PinmemberDarth_Sulfur14:28 5 Sep '05  
AnswerRe: Includes/Namespaces? PinmemberChristian Graus14:40 5 Sep '05  
GeneralRe: Includes/Namespaces? PinmemberDarth_Sulfur10:09 6 Sep '05  
GeneralRe: Includes/Namespaces? PinmemberChristian Graus11:08 6 Sep '05  
GeneralRe: Includes/Namespaces? PinmemberDarth_Sulfur11:27 7 Sep '05  
GeneralRe: Includes/Namespaces? PinmemberChristian Graus12:47 7 Sep '05  
GeneralRe: Includes/Namespaces? PinmemberDarth_Sulfur13:22 8 Sep '05  
GeneralRe: Includes/Namespaces? PinmemberChristian Graus13:27 8 Sep '05  
QuestionBug correction? Pinmemberpetermcwerner1:35 28 Jul '05  
GeneralBug PinsussPeter Werner8:18 27 Jul '05  
Generalwhy MakeLower() can not work PinmemberDavid 20086:30 17 Mar '04  
GeneralBug PinsussWicket_20:53 19 Feb '03  
GeneralRe: Bug Pinsusswicket_20:56 19 Feb '03  
Generala little optimization PinmemberKarstenK21:17 15 Apr '02  
GeneralRe: a little optimization PinsitebuilderUwe Keim21:23 15 Apr '02  
GeneralGood but a little bit slow PinmemberBraulio Díez22:33 22 Nov '01  

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.

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