GithubHelp home page GithubHelp logo

cpp17_implode_explode's Introduction

c++ join/split code snippet

Here are 3 codes snippet implementing string join and string split using C++ 11/14/17.

To use it, just include split_join.h or copy paste the function you want, there's no external dependencies, you can even rename it as your convenience.

You can also adapt it to work with basic_string only or force to return a vector.

string_join

Will join/merge/aggregate string/string_view with a separator and return the result into a string Exemple :

  vector<string> Vector1{ "C++","is","fun!" };
  string res1 = string_join(Vector1, " ");
  cout << "string_join(Vector1,' ') => " << res1 << endl;

Result : "C++ is fun!"

Works also with wstring

  vector<wstring> Vectorw1{ L"C++",L"is",L"widely",L"fun!" };
  wstring wres1 = string_join(Vectorw1, L" ");
  wcout << L"string_join(Vectorw1,' ') => " << wres1 << endl;

string_split

Will split/tokenize a string/string_view in multiple string/string_view in a collection/container

  vector<string> Vector2;
  string_split(string("C++ 17 will be fun!"), string(" "), Vector2);
  cout << "string_split  =>" << string_join(Vector2, "_") << endl;

Result : "C++_17_will_be_fun!" // I use string_join to display easily

Another syntax using return, specify input string type and output a template parameter on call

  auto Vector3 = string_split<string, vector<string> >("C++ 17 will be fun! by return", " ");
  cout << "string_split  =>" << string_join(Vector3, "_") << endl;

Result : C++_17_will_be_fun!_by_return

See main_demo.cpp for more examples.

Tested Compiler

Code running on VS2013/2015/2017 In order to have GCC working with string_view on C++ 17 Beta phase, it will require this extra code Else it works fine on GCC 4.9

using namespace std::experimental;
// Quick fix required to run on GCC , standard seems to cover it
// basic_string& operator+=( std::basic_string_view<CharT, Traits> sv);
string& operator+=(string &str,string_view sv){
    str+=string(sv);
    return str;
}

cpp17_implode_explode's People

Contributors

laurent-n avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

smallevilbeast

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.