C++ 将字符串中的第一个字母转换为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8530529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:44:11  来源:igfitidea点击:

Convert first letter in string to uppercase

c++stringmbcs

提问by user1065276

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"?

我有一个字符串:"apple". 如何仅将第一个字符转换为大写并以 的形式获取新字符串"Apple"

I can also have a string with multibyte characters.

我也可以有一个多字节字符的字符串。

What if the first character of the string is a multibyte character ?

如果字符串的第一个字符是多字节字符怎么办?

回答by Seth Carnegie

string str = "something";
str[0] = toupper(str[0]);

That's all you need to do. It also works for C strings.

这就是你需要做的。它也适用于 C 字符串。

回答by ybungalobill

I cannot use str[0] because, I can have string which has multibyte characters

我不能使用 str[0] 因为,我可以有包含多字节字符的字符串

I don't know of any CRT implementation that supports non-ASCII character classification and conversion. If you want to support Unicode then everything is much more complicated since "converting the first character to uppercase" may be meaningless in other languages. You have to use a Unicode librarywritten by experts for this.

我不知道任何支持非 ASCII 字符分类和转换的 CRT 实现。如果您想支持 Unicode,那么一切都会复杂得多,因为“将第一个字符转换为大写”在其他语言中可能毫无意义。为此,您必须使用由专家编写的Unicode 库

To illustrate how complicated it is, consider the following case in English. Converting the threecode-point sequence '?le' (with f-i ligature) shall breakthe first codepoint into twoseparate letters resulting in 'File'. Please note that the standard C/C++ interfaces for doing case classification and conversion don't take such cases into account, so it's even impossible to implement them to support Unicode correctly.

为了说明它有多复杂,请考虑以下英文案例。转换的3码点序列“?乐”(具有f结扎)应打破第一码点到2个致使“文件”单独字母。请注意,标准的 C/C++ 接口进行大小写分类和转换并没有考虑到这些情况,因此甚至不可能实现它们以正确支持 Unicode。

回答by HoKy22

Like what Carneigie said,

就像卡耐基说的那样,

string str = "something";
str[0] = toupper(str[0]);

but also remember to:

但也要记住:

#include <string>
#include <cctype>

all the way up

一路攀升

回答by Rye Bryant

#include <iostream>
using namespace std;

void capitalize (string &s)
{
    bool cap = true;

    for(unsigned int i = 0; i <= s.length(); i++)
    {
        if (isalpha(s[i]) && cap == true)
        {
            s[i] = toupper(s[i]);
            cap = false;
        }
        else if (isspace(s[i]))
        {  
            cap = true;
        }
    }
}

回答by Mateen Ulhaq

(Only works with 'ASCII' characters.)

(仅适用于“ASCII”字符。)

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;