标签: tolower

如何将std :: string转换为小写?

我想将a转换std::string为小写.我知道这个函数tolower(),但是在过去我遇到了这个函数的问题,并且它很难理想,因为使用a std::string会需要迭代每个字符.

有没有一种方法可以100%的时间运作?

c++ string c++-standard-library tolower

736
推荐指数
12
解决办法
83万
查看次数

将字符串转换为来自TitleCase C#的camelCase

我有一个字符串,我转换为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)

产生所需的输出

c# title-case camelcasing tolower

38
推荐指数
7
解决办法
5万
查看次数

在调用toupper(),tolower()等之前,是否需要转换为unsigned char?

不久之前,StackOverflow上有一个声誉很高的人在评论中写道,在调用(和类似的函数)之前必须抛出一个char-argument .unsigned charstd::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 charsigned char

c++ undefined-behavior language-lawyer toupper tolower

30
推荐指数
2
解决办法
2841
查看次数

为什么不能"转换(s.begin(),s.end(),s.begin(),tolower)"成功编译?

鉴于代码:

#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我写的函数替换它,它不再是错误.

c++ compiler-errors lowercase toupper tolower

28
推荐指数
4
解决办法
2万
查看次数

C++中的哪个版本?

鉴于string foo,我已经写了关于如何使用's 将字符转换为小写的答案cctypetolower

transform(cbegin(foo), cend(foo), begin(foo), static_cast<int (*)(int)>(tolower))
Run Code Online (Sandbox Code Playgroud)

但我已经开始考虑 localetolower,这可以这样使用:

use_facet<ctype<char>>(cout.getloc()).tolower(data(foo), next(data(foo), foo.size()));
Run Code Online (Sandbox Code Playgroud)
  • 是否有理由偏好其中一个而不是另一个?
  • 它们的功能是否完全不同?
  • 我的意思是tolower接受并返回一个int我认为只是一些过时的C东西的事实?

c++ string locale ctype tolower

20
推荐指数
2
解决办法
1301
查看次数

为什么putchar,toupper,tolower等采用int而不是char?

在C中,字符串是char(char *)数组,字符通常存储在其中char.我注意到libC中的一些函数将作为参数整数而不是char.

例如,让我们的职能toupper()tolower()两个使用int.手册页说:

如果c不是无符号字符值或EOF,则这些函数的行为是未定义的.

我的猜测是,有一个int,toupper并且tolower能够处理unsigned charEOF.但实际上EOF是在实践中(有没有关于它的值的规则?)一个可以用a存储的值char,并且由于这些函数不会转换EOF成其他东西,我想知道为什么toupper不简单地将char作为参数.

在任何情况下,为什么我们需要接受不是字符的东西(例如EOF)?有人能为我提供相关的用例吗?

这与fputc或类似putchar,也可以将int其转换为unsigned char无论如何.

我正在寻找这种选择的确切动机.我想说服,我不想回答,我不知道有一天有人问我.

c putchar toupper tolower

15
推荐指数
1
解决办法
2279
查看次数

用于C++字符串的tolower函数

是否有内置函数将C++字符串从大写字母转换为小写字母?如果没有将它转换为cstring并且在每个char上使用tolower是唯一的选择?

非常感谢你提前.

c++ tolower

14
推荐指数
2
解决办法
3万
查看次数

大写字母.R等效于excel"PROPER"功能

同事,

我正在查看类似于以下摘录的数据框:

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功能.

string r capitalization toupper tolower

9
推荐指数
4
解决办法
2万
查看次数

C++使用自定义区域设置的小写字符串

我一直试图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 ++进行编译(如果它与它有关).

我想知道这是否是实现我想做的正确方法,或者还有其他一些方法.

谢谢!

c++ locale tolower

8
推荐指数
1
解决办法
988
查看次数

tolower()功能无法在C99中工作

我正在使用哈佛的CS50设备并尝试将字符设为小写.我正在尝试使用该tolower()功能,但当我尝试使用它时,我收到了消息implicit declaration of function 'tolower' is invalid in C99.任何人都在关注为什么我会收到这条消息.我已经包括stdio.h,以及string.h.

c tolower cs50

7
推荐指数
2
解决办法
1万
查看次数