Tag Archives: Strings

Chomp a line in Python

When you read a file line by line in Python using

for line in f:

the carriage returns on the ends of the lines are retained.

The rstrip() method can be used to remove the trailing characters from a line. Without an argument, it strips all whitespace characters.

for line in f:
    line = line.rstrip()

rstrip() can be passed a string as its argument, in which case the characters in the string are stripped.

Note that the original variable is not modified – strings are immutable.

There is also lstrip() which strips the left-hand side of the string, and strip() which strips both sides.

Converting a String to an int or float in Python

Just use the int() or float() functions for this.

>>> int('42')
42
>>> float('42')
42.0
>>> float('3.1415')
3.1415
>>> int('3.1415')
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 10: '3.1415'
>>> float('3.141500e+00')
3.1415
>>> int('4.200000e+01')
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 10: '4.200000e+01'
>>> int(float('4.200000e+01'))
42

How to get a const char* or char* from a std::string

To get a const char* from a std::string, use its c_str() method:

const char* pc = s.c_str();
std::cout << pc << "\n";

To get a char*, you need to make a copy so that it’s safely non-const. Using the buffer of a std::vector is the easiest way of doing this as the buffer will automatically be deleted when it goes out of scope.

std::string s = "The quick, brown fox jumps over the lazy dog";
std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
pc = &v[0];
std::cout << pc << "\n";

Related

How to convert an int to a std::string in C++

Methods

  1. Use std::stringstream
  2. Use boost::lexical_cast
  3. Use folly::to()
  4. Use std::to_string (C++11)

Method 1: Use std::stringstream

#include <sstream>

template <typename T>
std::string to_string(const T& thing)
{
    std::stringstream ss;
    ss << thing;
    return ss.str();
}

template <>
std::string to_string<std::string>(const std::string& s)
{
    return s;
}

std::cout << ::to_string(n) << "\n";

Method 2: Use boost::lexical_cast

#include <boost/lexical_cast.hpp>
std::cout << boost::lexical_cast<std::string>(n) << "\n";

Reference: Boost.Lexical_Cast

Method 3: Use folly::to()

#include <folly/Conv.h>
std::cout << folly::to<std::string>(n) << "\n";

Reference: folly/Conv.h

Method 4: Use std::to_string (C++11)

std::cout << std::to_string(n) << "\n";

Related

Finding a substring in Python

You can simply use in to find out if a string contains another string:

line = "A license for my pet fish, Eric."
if 'license' in line:
    print '"license" is in line'

Use find() to get the position of the substring:

pos = line.find('license')
if pos != -1:
    print '"license" starts at position {0} of line'.format(pos)

How to split a string in C++

Java has String.split(), Python has string.split(), Perl has split. There is no simple string-splitting method in C++, but there are plenty of ways of doing it. Here are some methods:

  1. Put it in a stringstream and extract the tokens
  2. Put it in a stringstream and use getline() with a delimiter
  3. Use string::find progressively
  4. Use string::find_first_of progressively with a number of delimiters
  5. Use boost::split()
  6. Use boost::split_iterator
  7. Use boost::tokenizer
  8. Use boost::sregex_token_iterator
  9. Use pystring::split
  10. Use my C split function

1. Put it in a stringstream and extract the tokens

#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>

template <class Container>
void split1(const std::string& str, Container& cont)
{
    std::istringstream iss(str);
    std::copy(std::istream_iterator<std::string>(iss),
         std::istream_iterator<std::string>(),
         std::back_inserter(cont));
}

2. Put it in a stringstream and use getline() with a delimiter

#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>

template <class Container>
void split2(const std::string& str, Container& cont, char delim = ' ')
{
    std::stringstream ss(str);
    std::string token;
    while (std::getline(ss, token, delim)) {
        cont.push_back(token);
    }
}

3. Use string::find progressively

#include <string>
#include <algorithm>
#include <iterator>

template <class Container>
void split3(const std::string& str, Container& cont,
              char delim = ' ')
{
    std::size_t current, previous = 0;
    current = str.find(delim);
    while (current != std::string::npos) {
        cont.push_back(str.substr(previous, current - previous));
        previous = current + 1;
        current = str.find(delim, previous);
    }
    cont.push_back(str.substr(previous, current - previous));
}

4. Use string::find_first_of progressively with a number of delimiters

#include <string>
#include <algorithm>
#include <iterator>

template <class Container>
void split4(const std::string& str, Container& cont,
              const std::string& delims = " ")
{
    std::size_t current, previous = 0;
    current = str.find_first_of(delims);
    while (current != std::string::npos) {
        cont.push_back(str.substr(previous, current - previous));
        previous = current + 1;
        current = str.find_first_of(delims, previous);
    }
    cont.push_back(str.substr(previous, current - previous));
}

5. Use boost::split()

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

template <class Container>
void split5(const std::string& str, Container& cont,
              const std::string& delims = " ")
{
    boost::split(cont, str, boost::is_any_of(delims));
}

Reference: Function template split

6. Use boost::split_iterator

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

template <class Container>
void split6(const std::string& str, Container& cont,
              char delim = ' ')
{
    typedef boost::split_iterator<std::string::const_iterator> spliterator;
    std::string sdelim(1, delim);
    for (spliterator it = boost::make_split_iterator(str, 
               boost::first_finder(sdelim, boost::is_equal()));
               it != spliterator(); ++it) {
        cont.push_back(boost::copy_range<std::string>(*it));
    }
}

Reference: Function template make_split_iterator

7. Use Use boost::tokenizer

#include <string>
#include <algorithm>
#include <boost/tokenizer.hpp>

template <class Container>
void split7(const std::string& str, Container& cont,
              const std::string& delims = " ")
{
    typedef boost::char_separator<char> separator;
    boost::tokenizer<separator> tokens(str, separator(delims.c_str()));
    std::copy(tokens.begin(), tokens.end(), std::back_inserter(cont)); 
}

Reference: Tokenizer Class

8. Use boost::sregex_token_iterator

#include <string>
#include <algorithm>
#include <boost/regex.hpp>

template <class Container>
void split8(const std::string& str, Container& cont,
              const std::string delim = "\\s+")
{
    boost::regex re(delim);
    std::copy(boost::sregex_token_iterator(str.begin(), str.end(), re, -1),
            boost::sregex_token_iterator(), 
            std::back_inserter(cont)); 
}

Reference: regex_token_iterator

9. Use pystring::split()

#include <pystring.h>

template <class Container>
void split9(const std::string& str, Container& cont,
              const std::string delim = " ")
{
    std::vector<std::string> vec;
    pystring::split(str, vec, delim);
    std::copy(vec.begin(), vec.end(), std::back_inserter(cont));
}

Reference: pystring/pystring.h

10. Use my C split function

template <class Container>
void add_to_container(const char *str, size_t len, void *data)
{
    Container *cont = static_cast<Container*>(data);
    cont->push_back(std::string(str, len));
}

template <class Container>
void split10(const std::string& str, Container& cont, char delim = ' ')
{
    split(str.c_str(), delim, static_cast<split_fn>(add_to_container<Container>), &cont);
}

Reference: Split a string in C

An example program

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

int main()
{
    char str[] = "The quick brown fox jumps over the lazy dog";
    std::vector<std::string> words;
    split1(str, words);
    std::copy(words.begin(), words.end(),
         std::ostream_iterator<std::string>(std::cout, "\n"));
}
The
quick
brown
fox
jumps
over
the
lazy
dog

Related