Click here to Skip to main content
6,595,854 members and growing! (17,152 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » STL » General     Intermediate License: The Code Project Open License (CPOL)

Utilities for STL std::string

By jamesfancy

Utility functions for std::string
VC6, VC7, VC7.1, VC8.0Win2K, WinXP, Win2003, STL, VS.NET2003, VS2005, Dev
Posted:26 Mar 2007
Updated:18 Jun 2008
Views:80,014
Bookmarked:51 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
31 votes for this article.
Popularity: 5.47 Rating: 3.67 out of 5
9 votes, 29.0%
1
1 vote, 3.2%
2
5 votes, 16.1%
3
5 votes, 16.1%
4
11 votes, 35.5%
5

Downloads

Utilities for STL std::string

Lots of programmers have been familiar with some routines for string object, such as length, substring, find, charAt, toLowerCase, toUpperCase, trim, equalsIgnoreCase, startsWith, endsWith, parseInt, toString, split, and so on.

Now, if you are using STL and its string class std::string, how to do something which above routines do?

Of course, std::string supplies some methods to implements some routines above. They are,

length(), get the length of the string.
substr(), get a substring of the string.
at()/operator [], get the char at specified location in the string.
find/rfind(), search a string in a forward/backward direction for a substring.
find_first_of(), find the first character that is any of specified characters.
find_first_not_of(), find the first character that is not any of specified characters.
find_last_of(), find the last character that is any of specified characters.
find_last_not_of(), find the last character that is not any of specified characters.

Please refer document for more std::string's methods

Some routines are not implemented as std::string's methods, but we can find way in algorithm.h to do that. Of course, the existed methods of std::string are also used to implement them.

Transform a string to upper/lower case

std::transform(str.begin(), str.end(), str.begin(), tolower);
std::transform(str.begin(), str.end(), str.begin(), toupper);

Please refer document for detail of std::transform function

Trim spaces beside a string

Trim left spaces

string::iterator i;
for (i = str.begin(); i != str.end(); i++) {
    if (!isspace(*i)) {
        break;
    }
}
if (i == str.end()) {
    str.clear();
} else {
    str.erase(str.begin(), i);
}

Trim right spaces

string::iterator i;
for (i = str.end() - 1; ;i--) {
    if (!isspace(*i)) {
        str.erase(i + 1, str.end());
        break;
    }
    if (i == str.begin()) {
        str.clear();
        break;
    }
}

Trim two-sided spaces

Trim left spaces then trim right spaces. Thus two-sided spaces are trimed.

Create string by repeating character or substring

If you want create a string by repeating substring, you must use loop to implement it.

string repeat(const string& str, int n) {
    string s;
    for (int i = 0; i < n; i++) {
        s += str;
    }
    return s;
}

But if you need just to repeat character, std::string has a constructor.

string repeat(char c, int n) {
    return string(n, c);
}

Compare ignore case

It's funny. We should copy the two strings which attend compare. Then transform all of them to lower case. At last, just compare the two lower case strings.

StartsWith and EndsWith

StartsWith

str.find(substr) == 0;

If result is true, the str starts with substr.

EndsWith

size_t i = str.rfind(substr);
return (i != string::npos) && (i == (str.length() - substr.length()));

If result is true, the str ends with substr

There is another way to do that. Just get left substring or right substring to compare. Because I don't want to calculate if string's length is enough, so I use find and rfind to do that.

Parse number/bool from a string

For these routines, atoi, atol and some other C functions are OK. But I want use C++ way to do. So I choose std::istringstream. the class is in sstream.h.

A template function can do most excludes bool value.

template<class T> parseString(const std::string& str) {
    T value;
    std::istringstream iss(str);
    iss >> value;
    return value;
}

The template function can parse 0 as false and other number as true. But it cannot parse "false" as false and "true" as true. So I write a special function.

template<bool>
bool parseString(const std::string& str) {
    bool value;
    std::istringstream iss(str);
    iss >> boolalpha >> value;
    return value;
}

As you saw, I pass a std::boolalpha flag to the input stream, then the input stream can recognize literal bool value.

It is possible to use a similar way to parse hex string. This time I should pass a std::hex flag to the stream.

template<class T> parseHexString(const std::string& str) {
    T value;
    std::istringstream iss(str);
    iss >> hex >> value;
    return value;
}

To string routines

Like parsing from string, I will use std::ostringstream to get string from other kinds of value. The class is also in sstream.h. The relative 3 functions are followed.

template<class T> std::string toString(const T& value) {
    std::ostringstream oss;
    oss << value;
    return oss.str();
}
string toString(const bool& value) {
    ostringstream oss;
    oss << boolalpha << value;
    return oss.str();
}
template<class T> std::string toHexString(const T& value, int width) {
    std::ostringstream oss;
    oss << hex;
    if (width > 0) {
        oss << setw(width) << setfill('0');
    }
    oss << value;
    return oss.str();
}

Do you take note of setw and setfill? They are still flags which need an argument. std::setw allow the output thing in the stream occupy fixed width. If itself length is not enough, default uses space to fill. std::setfill is used to change the spaceholder. If you want control the alignment, there are std::left and std::right flags.

Oh, I forgot to tell you, setw and setfill need iomanip.h header file.

Split and tokenizer

I think split function should be implemented with a tokenizer. So I write a tokenizer at first. We can use find_first_of and find_first_not_of methods to get each token. Follows is nextToken method of Tokenizer class.

bool Tokenizer::nextToken(const std::string& delimiters) {
    // find the start character of the next token.
    size_t i = m_String.find_first_not_of(delimiters, m_Offset);
    if (i == string::npos) {
        m_Offset = m_String.length();
        return false;
    }

    // find the end of the token.
    size_t j = m_String.find_first_of(delimiters, i);
    if (j == string::npos) {
        m_Token = m_String.substr(i);
        m_Offset = m_String.length();
        return true;
    }

    // to intercept the token and save current position
    m_Token = m_String.substr(i, j - i);
    m_Offset = j;
    return true;
}

The whole Tokenizer is in the source code archive. You can download it at above. All other functions are still in the source code files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

jamesfancy


Member
James Fancy, is a software engineer from China. He enjoys and familiar with Java, C++ and some script languages.

If you can read Chinese word, here is one of James Fancy's BLOG:
http://hi.baidu.com/jamesfancy
Location: China China

Other popular STL articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 43 (Total in Forum: 43) (Refresh)FirstPrevNext
NewsBest wishes to you and your family, 绵阳的兄弟! PinmemberHuyong Zhao19:05 18 Jun '08  
GeneralRe: Best wishes to you and your family, 绵阳的兄弟! Pinmemberj.fan19:19 18 Jun '08  
Generalendswith bug PinmemberMember 441219022:03 3 Jun '08  
GeneralRe: endswith bug Pinmemberj.fan0:36 18 Jun '08  
Generalstd::string memory corruption on multi-processor Pinmemberana_v12322:20 29 Aug '07  
GeneralRe: std::string memory corruption on multi-processor PinmemberPablo Aliskevicius5:43 18 Jun '08  
QuestionWhy It is build error? It said &amp;amp;amp;quot;no matched function&amp;amp;amp;quot; [modified] Pinmemberliangtao6:00 21 Aug '07  
AnswerRe: Why It is build error? It said &amp;amp;amp;quot;no matched function&amp;amp;amp;quot; PinmemberJ.Fan.5:35 23 Aug '07  
Generaltransform or std::transform give compiing errors Pinmemberkezhu20:50 6 Jun '07  
GeneralRe: transform or std::transform give compiing errors [modified] PinmemberJ.Fan.3:29 7 Jun '07  
GeneralRe: transform or std::transform give compiing errors Pinmemberkezhu13:46 7 Jun '07  
Generaltrim left bug spaces followed by equal number chrs Pinmemberpjr18:10 13 May '07  
GeneralRe: trim left bug spaces followed by equal number chrs PinmemberJ.Fan.21:45 14 May '07  
GeneralRe: trim left bug spaces followed by equal number chrs PinmemberJ.Fan.17:03 16 May '07  
Generaltrim left bug for all spaces string. PinmemberCN_AHONG15:56 23 Apr '07  
GeneralRe: trim left bug for all spaces string. PinmemberJ.Fan.20:56 24 Apr '07  
Generalmissing .h file Pinmemberdlc06136:24 19 Apr '07  
GeneralRe: missing .h file PinmemberJ.Fan.5:46 20 Apr '07  
GeneralCool! PinmemberJohn R. Shaw4:15 6 Apr '07  
GeneralRe: Cool! PinmemberJ.Fan.5:36 7 Apr '07  
GeneralRe: Cool! [modified] PinmemberJohn R. Shaw9:36 8 Apr '07  
GeneralRe: Cool! PinmemberJ.Fan.16:42 8 Apr '07  
GeneralSome of my functions PinmemberGernot Frisch22:59 2 Apr '07  
GeneralRe: Some of my functions PinmemberJ.Fan.4:45 3 Apr '07  
Generalhehe~ It's better than yours. PinmemberCN_AHONG16:05 23 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jun 2008
Editor:
Copyright 2007 by jamesfancy
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project