我想将a转换std::string
为小写.我知道这个函数tolower()
,但是在过去我遇到了这个函数的问题,并且它很难理想,因为使用a std::string
会需要迭代每个字符.
有没有一种方法可以100%的时间运作?
我有一个字符串,我转换为TextInfo.ToTitleCase并删除下划线并将字符串连接在一起.现在我需要将字符串中的第一个字符和第一个字符更改为小写字母,由于某种原因,我无法弄清楚如何完成它.在此先感谢您的帮助.
class Program
{
static void Main(string[] args)
{
string functionName = "zebulans_nightmare";
TextInfo txtInfo = new CultureInfo("en-us", false).TextInfo;
functionName = txtInfo.ToTitleCase(functionName).Replace('_', ' ').Replace(" ", String.Empty);
Console.Out.WriteLine(functionName);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
结果:ZebulansNightmare
期望的结果:zebulansNightmare
更新:
class Program
{
static void Main(string[] args)
{
string functionName = "zebulans_nightmare";
TextInfo txtInfo = new CultureInfo("en-us", false).TextInfo;
functionName = txtInfo.ToTitleCase(functionName).Replace("_", string.Empty).Replace(" ", string.Empty);
functionName = $"{functionName.First().ToString().ToLowerInvariant()}{functionName.Substring(1)}";
Console.Out.WriteLine(functionName);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
产生所需的输出
不久之前,StackOverflow上有一个声誉很高的人在评论中写道,在调用(和类似的函数)之前必须抛出一个char
-argument .unsigned char
std::toupper
另一方面,Bjarne Stroustrup没有提到在C++ - Programming Language中这样做的必要性.他只是std::tolower
喜欢用
string name = "Niels Stroustrup";
void m3() {
string s = name.substr(6,10); // s = "Stroustr up"
name.replace(0,5,"nicholas"); // name becomes "nicholas Stroustrup"
name[0] = toupper(name[0]); // name becomes "Nicholas Stroustrup"
}
Run Code Online (Sandbox Code Playgroud)
(引用自该书,第4版.)
参考文献说输入需要表示为toupper
.对我来说,这听起来像每个unsigned char
以后都有,char
并且char
具有相同的大小.
那么这个演员是不必要的还是Stroustrup不小心?
编辑:libstdc ++手册提到输入字符必须来自基本源字符集,但不会强制转换.我想这是由@Keith Thompson的回复所涵盖的,他们都有积极的表现unsigned char
和signed char
?
鉴于代码:
#include <iostream>
#include <cctype>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s("ABCDEFGHIJKL");
transform(s.begin(),s.end(),s.begin(),tolower);
cout<<s<<endl;
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
呼叫没有匹配功能
transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)
什么是"未解决重载函数类型"是什么意思?
如果我用tolower
我写的函数替换它,它不再是错误.
鉴于string foo
,我已经写了关于如何使用's 将字符转换为小写的答案cctype
tolower
transform(cbegin(foo), cend(foo), begin(foo), static_cast<int (*)(int)>(tolower))
Run Code Online (Sandbox Code Playgroud)
但我已经开始考虑 locale
的tolower
,这可以这样使用:
use_facet<ctype<char>>(cout.getloc()).tolower(data(foo), next(data(foo), foo.size()));
Run Code Online (Sandbox Code Playgroud)
tolower
接受并返回一个int
我认为只是一些过时的C东西的事实?在C中,字符串是char(char *
)数组,字符通常存储在其中char
.我注意到libC中的一些函数将作为参数整数而不是char.
例如,让我们的职能toupper()
和tolower()
两个使用int
.手册页说:
如果c不是无符号字符值或EOF,则这些函数的行为是未定义的.
我的猜测是,有一个int
,toupper
并且tolower
能够处理unsigned char
和EOF
.但实际上EOF
是在实践中(有没有关于它的值的规则?)一个可以用a存储的值char
,并且由于这些函数不会转换EOF
成其他东西,我想知道为什么toupper
不简单地将char作为参数.
在任何情况下,为什么我们需要接受不是字符的东西(例如EOF)?有人能为我提供相关的用例吗?
这与fputc
或类似putchar
,也可以将int
其转换为unsigned char
无论如何.
我正在寻找这种选择的确切动机.我想说服,我不想回答,我不知道有一天有人问我.
是否有内置函数将C++字符串从大写字母转换为小写字母?如果没有将它转换为cstring并且在每个char上使用tolower是唯一的选择?
非常感谢你提前.
同事,
我正在查看类似于以下摘录的数据框:
Month Provider Items
January CofCom 25
july CofCom 331
march vobix 12
May vobix 0
Run Code Online (Sandbox Code Playgroud)
我想将每个单词的首字母大写,并降低每个单词的剩余字母.这将导致数据框类似于下面的数据框:
Month Provider Items
January Cofcom 25
July Cofcom 331
March Vobix 12
May Vobix 0
Run Code Online (Sandbox Code Playgroud)
总之,我正在寻找R等同于MS Excel中可用的ROPER功能.
我一直试图std::tolower()
用不同的语言环境调用,但似乎出现了问题.我的代码如下:
int main() {
std::locale::global(std::locale("es_ES.UTF-8"));
std::thread(&function, this); // Repeated some times
// wait for threads
}
void function() {
std::string word = "HeÉllO";
std::transform(word.begin(), word.end(), word.begin(), cToLower);
}
int cToLower(int c) {
return std::tolower(c, std::locale());
}
Run Code Online (Sandbox Code Playgroud)
所以当我尝试执行这个程序时,我得到:
terminate called after throwing an instance of 'std::bad_cast'
terminate called recursively
what(): std::bad_cast
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
虽然执行return std::tolower(c);
工作正常,但它只是将'标准'字符转换为较低的,而不是É
.
我有一些线程同时执行相同的功能,使用C++ 11并使用g ++进行编译(如果它与它有关).
我想知道这是否是实现我想做的正确方法,或者还有其他一些方法.
谢谢!
我正在使用哈佛的CS50设备并尝试将字符设为小写.我正在尝试使用该tolower()
功能,但当我尝试使用它时,我收到了消息implicit declaration of function 'tolower' is invalid in C99
.任何人都在关注为什么我会收到这条消息.我已经包括stdio.h
,以及string.h
.