PCRE2Plus icon indicating copy to clipboard operation
PCRE2Plus copied to clipboard

add some enhancement functions

Open xuboying opened this issue 9 years ago • 1 comments

chomp chop trim ltrim rtrim reverse join

buidin regex patterns e.g ip address

xuboying avatar May 22 '16 13:05 xuboying

#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <sstream>
#include <regex>
#include <boost/algorithm/string/join.hpp>

// Replace string -----------------------------------------------------------
// http://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string


bool replace(std::string& str, const std::string& from, const std::string& to) {
    size_t start_pos = str.find(from);
    if (start_pos == std::string::npos)
        return false;
    str.replace(start_pos, from.length(), to);
    return true;
}
// --------------------------------------------------------------------------




// Split string -------------------------------------------------------------
// http://stackoverflow.com/questions/236129/split-a-string-in-c

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

//-----------------------------------------------------------------------------
int main(){
    //Set Value
    std::string x = "HelloWorld";
    //Print Value
    std::cout<<x<<std::endl;
    //Get Type
    std::cout << typeid(x).name() << std::endl;

    //Modify Value
    x="Test";
    std::cout<<x<<std::endl;
    //Reverse
    std::reverse(x.begin(),x.end());
    std::cout<<x<<std::endl;

    //Upper Case
    std::transform(x.begin(), x.end(),x.begin(), ::toupper);
    std::cout<<x<<std::endl;

    //Lower Case
    std::transform(x.begin(), x.end(),x.begin(), ::tolower);
    std::cout<<x<<std::endl;

    //Sub str
    std::cout<<x.substr(1,3)<<std::endl;

    //Append
    x.append (" String");
    std::cout<<x<<std::endl;

    std::string y = "Monday Tuesday Wednesday";
    std::cout<<y<<std::endl;
    std::vector<std::string> AY=split(y,' ');
    std::cout<<* AY.begin()<<std::endl;
    for(auto I=AY.begin();I<AY.end();I++){
        std::cout<<*I<<std::endl;
    }
    // x function
    std::cout<<std::string(100,'a')<<std::endl;

    // Join function
    std::vector<std::string> list;
    list.push_back("aaa");
    list.push_back("bbb");
    std::string joined = boost::algorithm::join(list, ", ");
    std::cout << joined << std::endl;

    //int to string
    std::cout << std::to_string(42) << std::endl;


    //Regex 
    // http://en.cppreference.com/w/cpp/regex
    std::string s = "Some people, when confronted with a problem, think "
        "\"I know, I'll use regular expressions.\" "
        "Now they have two problems.";

    std::regex self_regex("REGULAR EXPRESSIONS",
            std::regex_constants::ECMAScript | std::regex_constants::icase);
    if (std::regex_search(s, self_regex)) {
        std::cout << "Text contains the phrase 'regular expressions'\n";
    }


    std::cin.get();
    return 0;
}

xuboying avatar May 28 '16 12:05 xuboying