C++ boost::algorithm::string::join()














































C++ boost::algorithm::string::join()



join():

     This function is included in the "boost/algorithm/string" library. This library contains some brilliant methods which help in accomplishing string manipulations that are lacking in STL library. 

This function is used to join various input strings. It joins the given input strings into one long string by adding a seperator between the strings i.e., the strings are concatenated by a seperator. The strings to be joined are provided in a container. Popularly used output containers are std::vector<std::string> or std::list<boost::iterator_range<std::string::iterator>>

header file:

boost/algorithm/string.hpp

syntax:

     join(input_container,seperator);

parameters:

    input_container : a container which holds the input strings

seperator : A constant (string) that seperates the joined strings

There is another variant present join_if() wich joins the strings if they satisfy a given predicate condition i.e., only those strings are joined that satisfy a given condition. The predicate maybe any library function or maybe specified by the user.

SAMPLE CODE :

#include<iostream>
#include<boost/algorithm/string.hpp>
#include<vector>

using namespace std;
using namespace boost::algorithm;


int main()
{
//declaring the strings to be joined
string str1 = "Hello";
string str2 = "how are you?";
string str3 = "This is boost";

//forming a container to hold the input strings
vector<string> v1;
v1.push_back(str1);
v1.push_back(str2);
v1.push_back(str3);

//joining the strings by seperating with a whitespace
string str4 = join(v1," ");
cout<<str4<<endl;

//joining the strings by seperating with a '-'
string str5 = join(v1,"-");
cout<<str5<<endl;

//joining the strings by seperating with '##'
string str6 = join(v1,"##");
cout<<str6<<endl;
}

OUTPUT:


EXPLANATION :

In the first case we are joining the three strings with a whitespace in between. In second case we are using '-' to seperate the joined strings. Whereas in the third case we are using a string "##".


More Articles of Md Safi Ur Rahman Khan:

Name Views Likes
C++ boost::algorithm::string::join() 577 0
C++ boost::algorithm::string::split() 606 0
C++ boost::algorithm::string::find_all() 646 0
C++ boost::algorithm::string::erase_tail() 248 0
C++ boost::algorithm::string::replace_tail() 211 0
C++ boost::algorithm::string::erase_head() 277 0
C++ boost::algorithm::string::replace_head() 209 0
C++ boost::algorithm::string::erase_all() 895 1
C++ boost::algorithm::replace_all() 2049 0
C++ boost::algorithm::string::erase_nth() 215 0
C++ boost::algorithm::string::replace_nth() 221 0
C++ boost::algorithm::string::replace_last() 246 0
C++ boost::algorithm::string::erase_last() 243 0
C++ boost::algorithm::string::erase_first() 215 1
C++ boost::algorithm::string::replace_first() 432 0
C++ boost::algorithm::string::find_token() 351 0
C++ boost::algorithm::string::find_tail() 200 1
C++ boost::algorithm::string::find_head() 212 0
C++ boost::algorithm::string::find_last() 300 1
C++ boost::algorithm::string::find_first() 749 1
C++ boost::algorithm::string::all() 224 1
C++ boost::algorithm::string::lexicographical_compare() 214 0
C++ boost::algorithm::string::equals() 378 0
C++ boost::algorithm::string::contains() 1821 0
C++ boost::algorithm::string::ends_with() 1282 0
C++ boost::algorithm::string::starts_with() 2058 0
C++ boost::algorithm::string::trim_if() 712 1
C++ boost::algorithm::string::trim() 3796 0
C++ boost::algorithm::string::trim_right_if() 522 1
C++ boost::algorithm::string::trim_left_if() 384 1
C++ boost::algorithm::string::trim_right() 282 3
C++ boost::algorithm::string::trim_left() 363 1
C++ boost::algorithm::string::to_lower() 736 1
C++ boost::algorithm::string::to_upper() 296 1
C++ Program to Implement Dequeue 1327 5
C++ Program to Implement Dequeue 241 3

Comments