青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

清風(fēng)竹林

ぷ雪飄絳梅映殘紅
   ぷ花舞霜飛映蒼松
     ----- Do more,suffer less

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

Collapse
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

Collapse
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

Collapse
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.

Collapse
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.

Collapse
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

Collapse
str.find(substr) == 0;

If result is true, the str starts with substr.

EndsWith

Collapse
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.

Collapse
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.

Collapse
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.

Collapse
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.

Collapse
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.

Collapse
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)

posted on 2009-05-15 15:45 李現(xiàn)民 閱讀(2047) 評(píng)論(1)  編輯 收藏 引用 所屬分類: 絕對(duì)盜版

評(píng)論

# re: Utilities for STL std::string 2009-08-28 09:49 xuxiangrong

學(xué)習(xí)!!!  回復(fù)  更多評(píng)論   

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美激情第五页| 国内外成人免费激情在线视频| 国产一区二区三区的电影| 性欧美大战久久久久久久久| 亚洲一区二区在线观看视频| 国产精品免费看片| 久久九九电影| 美女免费视频一区| 亚洲视频一区在线观看| 午夜精彩国产免费不卡不顿大片| 国产综合久久久久久鬼色| 亚洲少妇诱惑| 尤物精品在线| 一区二区高清视频| 一区二区三区在线高清| 亚洲精品国产系列| 99精品热6080yy久久| 国产精品欧美日韩一区二区| 久久久青草婷婷精品综合日韩| 蜜月aⅴ免费一区二区三区| 老鸭窝91久久精品色噜噜导演| 欧美激情一区二区三区高清视频 | 亚洲综合色噜噜狠狠| 国产亚洲综合在线| 欧美在线|欧美| 亚洲社区在线观看| 欧美日韩亚洲网| 免费不卡在线观看| 国产精品亚洲аv天堂网| 亚洲视频1区2区| 亚洲网站啪啪| 久久精品五月| 午夜日韩激情| 欧美日本亚洲韩国国产| 久久亚洲精品视频| 国产精品女人网站| 亚洲在线观看视频| 久久久久久97三级| 亚洲福利在线视频| 久久国产精品一区二区三区| 亚洲视频在线观看| 国产女主播一区二区| 亚洲九九爱视频| 亚洲欧洲在线视频| 久久久久国色av免费看影院| 性色av一区二区三区在线观看| 国产乱码精品一区二区三区不卡 | 日韩视频专区| 亚洲三级电影在线观看 | 久久久久这里只有精品| 亚洲高清av| 在线播放中文字幕一区| 欧美成人国产| 亚洲大胆视频| 亚洲综合导航| 国产欧美日韩在线视频| 久久蜜桃资源一区二区老牛 | 欧美日韩1234| 最新69国产成人精品视频免费| 在线观看国产日韩| 欧美日韩视频一区二区三区| 午夜日韩视频| 久久国产精品99精品国产| 欧美日韩亚洲一区二区三区四区| 亚洲一区中文| 亚洲品质自拍| 久久精品免费播放| 激情六月婷婷综合| 欧美日韩在线观看一区二区| 久久激情五月丁香伊人| 在线视频你懂得一区| 久久躁狠狠躁夜夜爽| 亚洲一区二区视频在线观看| 一区二区三区亚洲| 国产精品永久免费| 欧美日韩精品一区二区| 久久乐国产精品| 亚洲欧美日韩国产成人| 亚洲美女在线观看| 欧美大片免费| 亚洲深夜激情| 亚洲欧洲另类| 有码中文亚洲精品| 国产精品亚洲欧美| 国产精品chinese| 午夜欧美视频| 亚洲天堂av综合网| 久久综合给合久久狠狠色| 亚洲永久免费av| 日韩视频免费| 国产欧美一区二区三区在线老狼 | 午夜精品一区二区三区在线视| 亚洲激情av| 欧美日韩一区高清| 欧美激情第二页| 欧美激情国产高清| 欧美成人69av| 免费观看在线综合| 亚洲性av在线| 一区二区欧美亚洲| 亚洲精品午夜精品| 亚洲精品久久视频| 亚洲日本中文字幕区| 亚洲欧洲精品一区二区三区波多野1战4| 久久免费视频在线观看| 久久久久久黄| 裸体一区二区| 免费视频久久| 欧美激情一区三区| 久久国产手机看片| 久久精品国产一区二区三区| 欧美在线视频一区| 久久高清一区| 免费观看欧美在线视频的网站| 麻豆精品网站| 亚洲国产高清一区| 亚洲人成在线影院| 洋洋av久久久久久久一区| 一区二区三区蜜桃网| 亚洲免费视频在线观看| 午夜久久久久| 久久久综合视频| 欧美久久久久久久久久| 欧美午夜激情在线| 国产午夜精品久久久久久久| 欧美日韩专区在线| 欧美激情国产日韩| 欧美视频精品在线| 国产日韩精品视频一区| 激情六月综合| 99国产精品国产精品久久| 亚洲国产精品久久精品怡红院| 亚洲人成啪啪网站| 亚洲女人天堂成人av在线| 亚洲深夜激情| 久久精视频免费在线久久完整在线看| 欧美jizz19hd性欧美| 久久婷婷综合激情| 亚洲激情在线播放| 亚洲一区二区三区在线播放| 久久九九99视频| 欧美另类亚洲| 狠狠色丁香婷婷综合影院| 国产日韩亚洲| 亚洲精品在线免费观看视频| 午夜精品福利电影| 欧美激情小视频| 在线一区二区三区做爰视频网站| 欧美中文字幕在线| 欧美日韩妖精视频| 亚洲电影在线播放| 亚洲大胆在线| 亚洲欧美日韩国产一区| 免费成人在线观看视频| 亚洲亚洲精品在线观看| 免费一区视频| 国产亚洲在线观看| 这里只有精品丝袜| 小处雏高清一区二区三区 | 久久亚洲精选| 国产精品亚洲人在线观看| 99视频在线观看一区三区| 亚洲一区二区成人在线观看| 美女999久久久精品视频| 亚洲视频免费观看| 欧美激情bt| 亚洲高清在线观看| 老司机成人网| 久久av一区二区三区漫画| 国产精品久久久久久久电影| 国产一区二区三区在线观看网站| 制服诱惑一区二区| 亚洲第一天堂无码专区| 久久九九精品99国产精品| 国产精品色午夜在线观看| 99在线精品视频在线观看| 亚洲国产精品久久久久秋霞不卡| 亚洲伦理一区| 欧美成人资源| 亚洲日本中文字幕| 欧美福利视频| 老牛影视一区二区三区| 欧美调教视频| 一区二区三区四区精品| 最新国产の精品合集bt伙计| 美女在线一区二区| 亚洲大片一区二区三区| 美女久久一区| 久久婷婷国产综合精品青草| 在线观看视频一区二区| 另类专区欧美制服同性| 久久国产手机看片| 一区二区在线观看视频在线观看| 久久深夜福利| 久久久久亚洲综合| 91久久精品日日躁夜夜躁国产| 免费亚洲一区| 免费观看在线综合| 99在线精品观看| 亚洲图片激情小说|