VISH  0.2
Classes | Functions
String Utility functions

Classes

Functions


Function Documentation

template<typename E >
std::basic_string<E> crop_after_first ( std::basic_string< E > const &  s,
const E  c 
)

Crop all what follows after first occurance of character 'c'.

This is equivalent to the shell function ${s%c.*}

template<typename E >
std::basic_string<E> crop_after_last ( std::basic_string< E > const &  s,
const E  c 
)

Crop all what follows after last occurance of character 'c'.

This is equivalent to the shell function ${sc.*}

template<typename E >
std::basic_string<E> crop_before_first ( std::basic_string< E > const &  s,
const E  c 
)

Crop all what follows after first occurance of character 'c'.

This is equivalent to the shell function ${s#.*c}.

template<typename E >
std::basic_string<E> crop_before_last ( std::basic_string< E > const &  s,
const E  c 
)

Crop all what is before last occurance of character 'c'.

This is equivalent to the shell function ${s##.*c}

template<typename E >
std::basic_string<E> crop_postfix ( const std::basic_string< E > &  text,
const std::basic_string< E > &  postfix 
) [inline]

Remove some trailing text from a string.

Returns:
The original text will be returned if there was no postfix found.
See also:
right_crop()
template<typename E >
std::basic_string<E> crop_prefix ( const std::basic_string< E > &  text,
const std::basic_string< E > &  prefix 
) [inline]

Crop a prefix from some text, returning the unmodified text if not possible.

See also:
left_crop()
template<typename E , typename C >
size_t split ( std::basic_string< E > const &  s,
C &  container,
E const  delimiter,
bool  keepBlankFields = true,
E const  terminator = 0 
)

Splitting a string using a given delimiter.

Unfortunately the STL doesn't provide any direct way to achieve this. This split implementation is templatised and uses type deduction to determine what kind of string and container are being passed as arguments, which must must be some form of basic_string<> and any container that implements the push_back() method.

Note that the string type is not fully specified, allowing the traits and allocator to default. In some situations this may not be what you want and some compilers might not like it.

FROM: http://www.codeproject.com/KB/stl/Split_string.aspx

Example code:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

string tests[3] = {
    string(""),
    string("||three||five|"),
    string("|two|three||five|")
};

char* delimiter = "|";

for (int i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i)
{
    vector< string > x;
    size_t n = split(tests[i], x, delimiter[0], true);
    std::cout << n << "==" << x.size() << " fields" << std::endl;
    if (n)
    {
        copy(x.begin(), x.end(),
            ostream_iterator<string>(cout, delimiter));
        cout << endl;
    }
}
Examples:
WebVish.cpp.