我有一个 std::string 并希望第一个字母大写,其余字母小写。
我可以这样做的一种方法是:
const std::string example("eXamPLe");
std::string capitalized = boost::to_lower_copy(example);
capitalized[0] = toupper(capitalized[0]);
这将产生 capitalized
为:
"Example"
但也许有更直接的方法来做到这一点?
最佳答案
如果字符串确实只是一个单词,std::string capitalized = boost::locale::to_title (example)
应该这样做。否则,您得到的将非常紧凑。
编辑:刚刚注意到 boost::python
命名空间有一个 str
类和一个 capitalize()
方法,这听起来像是适用于多字串(假设您想要描述的内容而不是标题大小写)。但是,仅使用 Python 字符串来获得该功能可能不是一个好主意。
关于c++ - 如何将 C++ 字符串中的单词大写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15369910/