0

I have a string that contains unicode characters (I don't know if I am using the correct nomenclature). I can look character by character, but I can't compare directly to it:

What is the correct way to look for the presence of a wide character?

Here it is MWE, I get an error when trying to compare the character with for example, "α".

#include<iostream>

int main(){

    std::string s = " α a ";
    std::cout << "s = " << s << std::endl;
    for(std::string::iterator c = s.begin(); c != s.end(); ++c){
        if(*c == 'a') std::cout << "letter a" << std::endl; // ok
        if(*c == 'α') std::cout << "greek letter alpha" << std::endl; // error: character too large 
        std::cout << *c << std::endl;
    }   
    return 0;
}

I am using clang 3.5 in Linux.

What I want to do is to (copy) replace a character by a word. I could use regular expressions but that seems to be an overkill.

alfC
  • 14,261
  • 4
  • 67
  • 118
  • Why not use `std::wstring`? And `L`α'`? – Ed Heal Sep 19 '15 at 05:54
  • @EdHeal, thanks. What if the framework is already with `std::string`? Can I convert the `std::string` to `std::wstring` to do this operation? (`std::wstring s2(s);` gives an error). – alfC Sep 19 '15 at 05:57
  • 2
    @alfC- See http://stackoverflow.com/questions/6691555/converting-narrow-string-to-wide-string – Ed Heal Sep 19 '15 at 05:59
  • 3
    @EdHeal, wow, it looks like I opened a Pandora's box, I have to change a lot of the code to `wstring`, `wchar` and `std::wcout`, etc. – alfC Sep 19 '15 at 06:02
  • 2
    ... Welcome to the world of writing programs that work in any part of the world – Ed Heal Sep 19 '15 at 06:04
  • .. PS. Make the error messages work in the native tongue as well. And do not forget that in the arab world people write right to left – Ed Heal Sep 19 '15 at 06:05
  • @EdHeal, very true. (I am not doing internationalization though). However, string seems to be doing the job well until now, it just looks like character processing is more difficult. What I am seeing is that perhaps I have to avoid individual character accessing and deal with strings only. For example if I want to replace `α` by `alpha`, I could just do `boost::replace_all(s, "α", "alpha")` and it will works because there is no individual character involved. That is the work around I found for now. – alfC Sep 19 '15 at 09:15

0 Answers0