Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by Fruny
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]


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
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?

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

Share this post


Link to post
Share on other sites
Quote:
Original post by load_bitmap_file
Quote:
Original post by Fruny
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]


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 this post


Link to post
Share on other sites
Quote:
Original post by load_bitmap_file
EDIT: 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 ForwardIterator
tolower(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 this post


Link to post
Share on other sites
Quote:
Original post by MrEvil
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")) );

?


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!