Is there some quick C++ way I can do something similar to string::find ,
but case insensitive ? 5 16560
"Nils O. Selåsdal" <NO*@Utel.no> wrote in message
news:b_******** **********@news 2.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((unsign ed char)ch1) == toupper((unsign ed char)ch2);
}
size_t ci_find(const string& str1, const string& str2)
{
string::iterato r 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
John Harrison wrote: "Nils O. Selåsdal" <NO*@Utel.no> wrote in message news:b_******** **********@news 2.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((unsign ed char)ch1) == toupper((unsign ed char)ch2); }
size_t ci_find(const string& str1, const string& str2) { string::iterato r 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_i terator, it seems to work fine.
Thanks.
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((unsign ed char)ch1) == toupper((unsign ed 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
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 ..
"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_trait s<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_trait s<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_stri ng<char, ichar_traits<ch ar> > s1("Hello");
std::basic_stri ng<char, ichar_traits<ch ar> > s2("HELLO");
assert(s1==s2);
assert(s1.find( s2)==0); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 = string.find(files, 'the-other')
|
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 = string.find(file(os.path.join(root,fname),
'rb').read(), 'THIS')
print find
|
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, i don't know but
nevermind that) but what if a searched_string == '' ? Then
string.find(substring,searched_string) == 0 ?
example:
|
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 problem: In ParamGenerate() I want to
find all whitespaces before a certain point in one string. Thus I use
the string::find() function. However it seems...
|
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 describe if applied to a sequence of length
items. It returns a tuple of three integers; respectively
these are the /start/ and /stop/ indices and...
| |
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 happening? How do I fix this?
Thanks
// stack trace
msvcp71d.dll!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>...
|
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 (-1).
I can print out the config string at the beginning of CleanCfgFile(),
and the strings are there exactly the way I'm looking for them. So,...
|
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 be dependant on which string is searched for.
On my system, using gcc 4.1.2, I get:
pos1=2
problem because pos1=0
|
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;
loc = str.find( "string" );
loc = str.find( "string", loc + 1, 6 );
|
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...
|
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...
| |
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |