472,354 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

case insensitive string::find

Is there some quick C++ way I can do something similar to string::find ,
but case insensitive ?
Jul 22 '05 #1
5 16432

"Nils O. Selåsdal" <NO*@Utel.no> wrote in message
news:b_******************@news2.e.nsc.no...
Is there some quick C++ way I can do something similar to string::find ,
but case insensitive ?


You can use the predicate version of std::search

bool ci_equal(char ch1, char ch2)
{
return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
}

size_t ci_find(const string& str1, const string& str2)
{
string::iterator pos = search(str1. begin ( ), str1. end ( ), str2.
begin ( ), str2. end ( ), ci_equal);
if (pos == str1. end ( ))
return string::npos;
else
return pos - str1. begin ( );
}

Untested code.

John
Jul 22 '05 #2
John Harrison wrote:
"Nils O. Selåsdal" <NO*@Utel.no> wrote in message
news:b_******************@news2.e.nsc.no...
Is there some quick C++ way I can do something similar to string::find ,
but case insensitive ?

You can use the predicate version of std::search

bool ci_equal(char ch1, char ch2)
{
return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
}

size_t ci_find(const string& str1, const string& str2)
{
string::iterator pos = search(str1. begin ( ), str1. end ( ), str2.
begin ( ), str2. end ( ), ci_equal);
if (pos == str1. end ( ))
return string::npos;
else
return pos - str1. begin ( );
}

Untested code.

Change to string::const_iterator, it seems to work fine.
Thanks.
Jul 22 '05 #3
John Harrison schrieb:
"Nils O. Selåsdal" <NO*@Utel.no> wrote:
Is there some quick C++ way I can do something similar to string::find ,
but case insensitive ?

You can use the predicate version of std::search

bool ci_equal(char ch1, char ch2)
{
return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
}


Caveat: that predicate will not work reliably for non-English strings.
e.g. in French accents are usually stripped off of capital letters, so
an 'E' can be equivalent (equal modulus case) to either 'e', 'é', 'è',
or 'ê'. In German, the letter 'ß' only exists in lower case, and the
correct capitalisation is "SS", while the reverse conversion is
ambiguous (some "SS" convert to "ss", others to 'ß').

It still doesn't catch all of those issues, but in most cases tolower()
is the better variant in my experience.

While 100% correct case conversions cannot be done in some languages
without knowledge of their spelling rules and exceptions to them, the
'ß' case IMHO shows that signatures like charT toupper( charT ) have
been designed with ignorance (no offence) towards languages where a
1-char-to-1-char conversion is not possible. Wide character support
doesn't alleviate this at all :-(((
Standardised whole-string case conversion functions would be deerly
needed, even if it's left to the implementation or the application to
implement them for a particular locale.

Regards,
Malte
Jul 22 '05 #4
On Tue, 19 Oct 2004 15:03:14 +0200, Malte Starostik wrote:
John Harrison schrieb:
"Nils O. Selåsdal" <NO*@Utel.no> wrote:
Is there some quick C++ way I can do something similar to string::find ,
but case insensitive ?
.... 1-char-to-1-char conversion is not possible. Wide character support
doesn't alleviate this at all :-(((
Standardised whole-string case conversion functions would be deerly
needed, even if it's left to the implementation or the application to
implement them for a particular locale.

Fortunatly for me, I need only care about ascii a-z,A-Z ..
Jul 22 '05 #5
"Nils O. Selåsdal" <NO*@Utel.no> wrote in message news:b_5dd.2715
Is there some quick C++ way I can do something similar to string::find ,
but case insensitive ?


See John's post for an excellent solution.

A more elaborate possibility is to create a new traits class, similar to
std::char_traits<char>, that compares without sensitivity. But now all
string compare operations are case insensitive, so you might find yourself
trying to use std::search to compare with case sensitivity! I haven't tried
myself yet, but seems this should work:

class ichar_traits : public std::char_traits<char> {
public:
static bool eq(char, char);
static bool lt(char, char);
static int compare(const char *, const char *, size_t n);
static const char * find(const char *, size_t n, char);
};

std::basic_string<char, ichar_traits<char> > s1("Hello");
std::basic_string<char, ichar_traits<char> > s2("HELLO");
assert(s1==s2);
assert(s1.find(s2)==0);
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: hokiegal99 | last post by:
How do I say: x = string.find(files, 'this', 'that', 'the-other') currently I have to write it like this to make it work: x = string.find(files, 'this') y = string.find(files, 'that') z =...
10
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find =...
5
by: MyHaz | last post by:
OK i find this a quark in string.find that i think needs some consideration. Normally if a substring is not in the searched_string then string.find returns -1 (why not len(searched_string) + 1,...
3
by: Chris Mantoulidis | last post by:
I posted this here one day ago but it seems like it hasn't been put up for some unknown reason. That gives me a chance to say things a bit better in this post. 1st of all let's desribe the...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
0
by: Ian Lazarus | last post by:
Hello, A call to std::basic_string<wchar_t>::find(...) from within a class library is not working as expected. The value passed to find is 0x61, but the value it receives is 0xE961. What's...
5
by: PaulH | last post by:
I have a function that is stripping off some XML from a configuration file. But, when I do a search for the pieces I want to strip, the std::string::find() function always returns std::string::npos...
11
by: Ko van der Sloot | last post by:
Hello I was wondering which behaviour might be expected (or is required) for the following small program. I would expect that find( "a", string::npos ) would return string::npos but is seems to...
2
by: Soneji | last post by:
Just a quickie today... Is there another ( better ) way to get the second value from this string::find example: std::string str( "More strings of the string variety" ); size_t loc; ...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.