Pacific-Design.com

    
Home Index

1. Cpp 11

+ Boost Libraries

+ Highlighter

+ Read Random Line

+ Text Files

+ cgi-echo

+ cmake

Cpp 11 /

Tokenize String


  std::string s = "kevin=:thomas=:duraj";
  std::string delimiter = "=:";
  
  size_t pos = 0;
  std::string token;

  while ((pos = s.find(delimiter)) != std::string::npos) {
      token = s.substr(0, pos);
      std::cout << token << std::endl;
      s.erase(0, pos + delimiter.length());
  }
  std::cout << s << std::endl;

Regex - Replace


  std::string text = "Quick brown fox";
  std::regex vowel_re("a|e|i|o|u");
 
  // write the results to an output iterator
  std::regex_replace(std::ostreambuf_iterator(std::cout),
                      text.begin(), text.end(), vowel_re, "*");
 
  // construct a string holding the results
  std::cout << '\n' << std::regex_replace(text, vowel_re, "[$&]") << '\n';

Regex - Replace with Match


  std::string str = "kevin=duraj";
  regex word_regex("\\w+=");

  string res1 = std::regex_replace (str, word_regex, "|$0");
  res1.erase(0, 1); // erease first | character

  if(format == "csv") {
    string result_csv = result;
    boost::replace_all(result_csv, "\n", ",");
    csv << result_csv << endl; 
  }
  // returns kevin