# std::string class, is there any built in lowercase converter?

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi im using std::string, and i'm doing a string sort and a string search(by dividing and checking if the string im looking for is >, =, < then the marker). Anyway the problem is when sorting capitals with lower case strings, it puts captials in front of lower case, eg: Hello and hi So I was wondering if the std:string class comes with some kind of toLowerCase function? Thanks

##### Share on other sites
No, it doesn't. The reason is that std::string is locale independent and that different languages (locales) have different conventions regarding such conversions. It is easy to do, though, by running one of the tolower() functions (there are several, which causes some trouble with generic code) over each character. Here is a way for you to do it:

#include <cctype>#include <string>std::string str = "Hello World!";for(int i=0; i<str.size();++i)   str = tolower(str);

It's not the ideal way, but that will do the work. I'm sure somebody will come along shortly to post the transform + locale + functor version [grin]

##### Share on other sites
Quote:
 Original post by FrunyIt's not the ideal way, but that will do the work. I'm sure somebody will come along shortly to post the transform + locale + functor version [grin]

Well, here's a halfway-there version:

std::transform(theString.begin(), theString.end(), theString.begin(), tolower);

Pretty much the same thing as Fruny's except you don't have to manually write the for loop yourself. Doesn't use any locale/functor goodness.

EDIT: Now I'll join Fruny in waiting for someone to post the transform + locale + functor version [grin]

##### Share on other sites
hmm on the net, it says tolower() arg is an ascii integer, and returns the lower ascii integer.

does the class have some kind of ascii to string converter?

Thanks

##### Share on other sites
the tolower you are refering to is the C standard library version
look for std::tolower ie, C++ Standard Library

##### Share on other sites
Quote:
 Original post by johnnyBravohmm on the net, it says tolower() arg is an ascii integer, and returns the lower ascii integer.does the class have some kind of ascii to string converter?

The ASCII integer is what you want. Read up on what std::transform does; it operates on an element at a time.

##### Share on other sites
Alternatively, you could use the Boost String Algorithms to do this. :)

##### Share on other sites
Quote:
Quote:
 Original post by FrunyIt's not the ideal way, but that will do the work. I'm sure somebody will come along shortly to post the transform + locale + functor version [grin]

Well, here's a halfway-there version:

std::transform(theString.begin(), theString.end(), theString.begin(), tolower);

Pretty much the same thing as Fruny's except you don't have to manually write the for loop yourself. Doesn't use any locale/functor goodness.

EDIT: Now I'll join Fruny in waiting for someone to post the transform + locale + functor version [grin]

Erm...

#include<functional>#include<string>std::transform(theString.begin(), theString.end(), theString.begin(), std::bind2nd(&std::tolower, locale("fr_FR")) );std::transform(theString.begin(), theString.end(), theString.begin(), std::bind2nd(&std::tolower, locale("POSIX")) );

?

PS. Indeed, Boost String Algorithms are most excellent

##### Share on other sites
Quote:
 Original post by load_bitmap_fileEDIT: Now I'll join Fruny in waiting for someone to post the transform + locale + functor version [grin]

#include <boost/bind.hpp> // bind#include <locale>         // tolower#include <algorithm>      // transform#include <string>template < typename ForwardIterator >inline ForwardIteratortolower(ForwardIterator first, ForwardIterator last,		const std::locale& locale_ref = std::locale()) {   using namespace boost;   using namespace std;   typedef typename std::iterator_traits<ForwardIterator>::value_type       value_type;   return transform(first, last, first, 		bind(&std::tolower<value_type>, _1, cref(locale_ref)));}template < typename CharT, typename Traits, typename Alloc >inline std::basic_string<CharT, Traits, Alloc>&tolower(std::basic_string<CharT, Traits, Alloc>& s,		const std::locale& locale_ref = std::locale()) {	   tolower(s.begin(), s.end(), locale_ref);   return s;}

##### Share on other sites
Quote:
 Original post by MrEvilErm... #include#includestd::transform(theString.begin(), theString.end(), theString.begin(), std::bind2nd(&std::tolower, locale("fr_FR")) );std::transform(theString.begin(), theString.end(), theString.begin(), std::bind2nd(&std::tolower, locale("POSIX")) );?

Well that shouldn't compile, apart from missing some headers [grin], std::tolower in the header locale is a template function, you need to do an explicit instantiation when giving the address to std::bind2nd

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011388
• ### Who's Online (See full list)

There are no registered users currently online

×