Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to strip all non-numeric characters from string in C++

2 Answers

0 votes
#include <iostream>
#include <regex>

using namespace std;

int main() {
    string s = "2a1-0/R@a9f#4K$$cC3K^htPam8vlQWhJ";

    s = std::regex_replace(s, std::regex(R"([\D])"), "");
    
    std::cout << s;
}



/*
run:

2109438

*/

 



answered Jun 15, 2020 by avibootz
0 votes
#include <iostream>
#include <algorithm>

using namespace std;

bool not_a_digit(char ch) {
    return '0' <= ch && ch <= '9';
}

std::string remove_non_numeric(const std::string &s) {
    string result;
    copy_if(s.begin(), s.end(), std::back_inserter(result), not_a_digit);
    
    return result;
}

int main() {
    string s = "2a1-0/R@a9f#4K$$cC3K^htPam8vlQWhJ";

    s = remove_non_numeric(s);
    
    std::cout << s;
}



/*
run:

2109438

*/

 



answered Jun 15, 2020 by avibootz
edited Jun 15, 2020 by avibootz

Related questions

2 answers 133 views
1 answer 120 views
2 answers 189 views
2 answers 253 views
2 answers 239 views
1 answer 148 views
...