Home > Blockchain >  C std::string::at()
C std::string::at()

Time:04-25

I want to print the first letter of a string.

#include <iostream>
#include <string>

using namespace std;

int main() {
  string str = "다람쥐 헌 쳇바퀴 돌고파.";
  cout << str.at(0) << endl;
}

I want '다' to be printed like java, but '?' is printed.

How can I fix it?

CodePudding user response:

That text you have in str -- how is it encoded?

Unfortunately, you need to know that to get the first "character". The std::string class only deals with bytes. How bytes turn into characters is a rather large topic.

The magic word you are probably looking for is UTF-8. See here for more infomation: How do I properly use std::string on UTF-8 in C ?

If you want to go down this road yourself, look here: Extract (first) UTF-8 character from a std::string

  • Related