如何从c++中的字符串中减去字符?

发布于 2025-01-26 14:43:01 字数 157 浏览 1 评论 0原文

您好,我想知道如何从字符串中划分字符串,

例如

字符串s =“ 124ab” 我可以通过使用Sstream轻松提取整数,但我不知道如何提取

要从S提取AB的字符串; 我有大量的字符串,它们没有任何规则。 S可以是“ 3456fdhgab”或“ 34a678”

Hello I want to know how to subract string from string

For example

If string s = "124ab"
I can easily extract integer by using sstream but I don't know how to extract string

I want to extract ab from s;
I have tons of string and they don't have any rule.
s can be "3456fdhgab" or "34a678"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆 2025-02-02 14:43:01

您可以使用 std :: isDigit 检查字符是否是数字。您可以使用 erase-remove idiom 。

因为std :: isDigit具有超载,必须将其包裹在lambda中才能在算法中使用:

#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>

int main() {
    std::string inp{"124ab"};
    inp.erase(std::remove_if(inp.begin(),inp.end(),[](char c){return std::isdigit(c);}),inp.end());
    std::cout << inp;
}

output

ab

由于您要求使用Stringstream,因此您可以使用自定义操作员&lt;&lt;&lt;

#include <string>
#include <iostream>
#include <cctype>
#include <sstream>

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}

输出

ab
ab

You can use std::isdigit to check if a character is a digit. You can use the erase-remove idiom to remove characters that are digits.

Because std::isdigit has an overload it has to be wrapped in a lambda to be used in the algorithm:

#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>

int main() {
    std::string inp{"124ab"};
    inp.erase(std::remove_if(inp.begin(),inp.end(),[](char c){return std::isdigit(c);}),inp.end());
    std::cout << inp;
}

Output:

ab

And because you asked for using a stringstream, here is how you can extract non-digits with a custom operator<<:

#include <string>
#include <iostream>
#include <cctype>
#include <sstream>

struct only_non_digits {
    std::string& data;    
};

std::ostream& operator<<(std::ostream& os, const only_non_digits& x) {
    for (const auto& c : x.data){
        if (std::isdigit(c)) continue;
        os << c;
    }
    return os;
}    

int main() {
    std::string inp{"124ab"};
    std::cout << only_non_digits{inp} << "\n";
    std::stringstream ss;
    ss << only_non_digits{inp};
    std::cout << ss.str() << "\n";
}

Output:

ab
ab
无所谓啦 2025-02-02 14:43:01
string removeNumbers(string str)
{
    int current = 0;
    for (int i = 0; i < str.length(); i++)
    {
        if (!isdigit(str[i])) {
            str[current] = str[i];
            current++;
        }
    }
    return str.substr(0, current);
}

int main()
{
     string str;
     cout <<"Enter the string : " <<endl;
     getline(cin,str);
     cout <<"Modified string : " <<removeNumbers(str) <<endl;

}
string removeNumbers(string str)
{
    int current = 0;
    for (int i = 0; i < str.length(); i++)
    {
        if (!isdigit(str[i])) {
            str[current] = str[i];
            current++;
        }
    }
    return str.substr(0, current);
}

int main()
{
     string str;
     cout <<"Enter the string : " <<endl;
     getline(cin,str);
     cout <<"Modified string : " <<removeNumbers(str) <<endl;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文