[英]how to remove leading lower case letters, underscores from a string in c++?
here i have a string but i need to remove the lower case letter only ie, mi. 在这里,我有一个字符串,但是我只需要删除小写字母,即mi。
primitive code is: 原始代码是:
bool check(char c) { return !(std::isdigit(c) ||std::isalpha(c)); }
int main() {
std::string str = "m_ivecCadCurveEdges_Pattern";
str.erase(std::remove_if(str.begin(), str.end(), check),str.end());
for (std::string::size_type i=0; i<str.length(); ++i)
{
//if (!std::islower(str.at(i) && str.at(i) != '_')
if (!std::islower(str.at(i)))
{
str.erase(0, i);
break;
}
}
if (std::isdigit(str.at(0)))
str= "N" + str;
std::cout<<str;
}
I want to break the loop once it reach B in the string. 我想在到达字符串B后打破循环。
Looking at the title I suggest you use a combination of remove_if and islower . 查看标题,我建议您结合使用remove_if和islower 。 However the question body implies that you may need to remove only the longest prefix of lower case characters.
但是,问题正文暗示您可能只需要删除小写字符的最长前缀。 If that is the case you will need a combination of std::string#erase and find_if for isupper .
如果是这样的话,你需要的组合的std :: string#擦除和find_if的isupper 。
erase-remove is good if you wanted the whole string processed, but you ask to have only the leading lower-case character changed. 如果要处理整个字符串,则“擦除-删除”效果很好,但是您要求仅更改前导小写字符。 For that, just change your "if" code:
为此,只需更改您的“ if”代码:
if (!std::islower(str.at(i))
{
str.erase(0, i);
break;
}
Your cout should be after the for loop. 您的提示应该在for循环之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.