Convert first letter in string to uppercase

后端 未结 5 1095
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 23:27

I have a string: \"apple\". How can I convert only the first character to uppercase and get a new string in the form of \"Apple\"?

I can al

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 00:02

    (Only works with 'ASCII' characters.)

    std::wstring s = L"apple";
    
    if(islower(s.at(0) <= 'z' ? s.at(0) : 'A'))
        s[0] += 'A' - 'a';
    

    Or if you are feeling fancy and feel like torturing any future readers of your code:

    std::wstringstream wss;
    wss << std::uppercase   << s[0]
        << std::nouppercase << s.substr(1);
    wss >> s;
    

提交回复
热议问题