473,671 Members | 2,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Portable way to compare strings using case-insensitive

Hi! The following code snippet:

namespace {
bool charCompare(cha r a, char b) {
return tolower(a) < tolower(b);
}
}

bool compareString(c onst std::string & s1, const std::string & s2)
{
return
std::lexicograp hical_compare(s 1.begin(),s1.en d(),s2.begin(), s2.end(),charCo mpare);
}

presents a compareString function to compare strings ignoring the case

compareString(" yes","YES") --true;

but

compareString(" ahá","AHÁ") --false;

how can I make it more portable to using accents, for example?

what are your background in this issue? what about using a Locale
class? (actually, I know nothing about locales in C++)

a possible idea is using classes to deal with character encode.
something like that:

compareString<I SO8859_1>("ahá" ,"AHÁ")

and it will delegate to charCompare<T>

an alternative is to use an overload like that:
bool compareString(c onst std::string & s1, const std::string & s2,
const CharConv & conv)

and CharConv can be a conversion table or a class that implements
tolower() and toupper() for each desired character codification

can you comment the ideas above?

any better ideas?

Diego Martins
HP

Aug 22 '06 #1
0 2304

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

Similar topics

2
76523
by: J.W. | last post by:
How do I compare strings in javascript? The "==" double equals or "!=" doesn't seem to work in this case. I'm sure string comparison has been explained before but searching Google didn't find any code that looked similar to this one: ----- function CheckPassword(val,val2, str) { var strInput = new String(val.value);
11
11456
by: xuatla | last post by:
Hi, I want to compare two strings regardless of the lowercase or uppercase. For example, "txt" same as "TXT", "Txt", ... I know that there's stricmp in some c++ that can perform a lowercase comparison. But when I use <cstring>, I can't find such function. Do you know any other standard c++ function(s) can do this task? Thanks,
19
9506
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I have tried to print out by debug.writeline(). But the "==" operator results false, and string.Compare() results true. Somebody helps me!
2
1224
by: Roger Twomey | last post by:
I have 16 string values that I need to compare to each other. No two are supposed to hold the same value (if they do something is amiss and I need to abort the operation). Short of 'IF Then' comparisons for every possible combination (do-able but very cludgy) is there not a better way to code this? Any idea will be considerted. Thanks.
3
1980
by: gerards_ | last post by:
Hi all, What I am trying to do is to String Compare just the first 6 characters of a string. For example: For the compare string "Powama" I want to return true for any strings that contains these first 6 characters (in order).
5
37810
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
131
6110
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write portable C code - *not only* because, in a 'C sense', I
28
3396
by: James Brown | last post by:
All, I have a series of characters which I need to convert to integer values. Each character is read in turn from a function 'nextch', and hex-digits are identified by the isxdigit function - so I'm looking at '0' - '9', 'A' - 'Z' and 'a' - 'z'. Here is what I've got: int num = 0;
50
20293
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
6
17590
by: shapper | last post by:
Hello, I want to compare two strings in a Linq Query. In this case "Car", "cAr", "CAR" would all be the same. Should I use ==, equals, ... ? What is the best way to do this?
0
8485
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8403
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.