Comparing UTF-8 with a substring

I'm trying to print out all the letters in a string until a curly quote is found. However I can't compare a sub-string too a UTF-8 code for comparisons, why is that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;



int main()
{
	string words = u8"James says \u201CHi Everyone\u201D ";
	for (int i = 0; i < words.length(); i++)
	{
		if (words.substr(i, 1) == u8"\u201C")
		{
			break;
		}
		cout << words.substr(i, 1);
	}
	return 0;
}




Because your substring's length is too short.

Andy

len = 3
str = “
bytes = e2 80 9c


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
    string str = u8"\u201C";
    cout << "len = " << str.length() << "\n";
    cout << "str = ";    
    for (int i = 0; i < str.length(); i++)
    {
        cout << str.substr(i, 1);
    }
    cout << "\n";        
    cout << "bytes =";
    cout << hex;
    const char* psz = str.c_str();
    for (int i = 0; i < str.length(); i++)
    {
        cout << " " << (int)(unsigned char)psz[i];
    }
    cout << "\n";
    return 0;
}
Last edited on
Topic archived. No new replies allowed.