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

Benjamin

靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
隨筆 - 398, 文章 - 0, 評論 - 196, 引用 - 0
數據加載中……

c++ 字符串工具類

  1 #ifndef STRINGUTILS_H__
  2 #define STRINGUTILS_H__
  3 
  4 #include <sstream>
  5 #include <string>
  6 #include <vector>
  7 #include <algorithm>
  8 #include <stdarg.h>
  9 
 10 /**
 11 * Collection of various helper methods for strings.
 12 *
 13 * \sa std::string
 14 *
 15 * \todo    Test, test, test!
 16 */
 17 namespace StringUtils {
 18 
 19     /**
 20     * Converts the string \a str to lowercase.
 21     * \param str   String to convert.
 22     * \return Lowercase version of \a str.
 23     */
 24     static std::string lowercase(const std::string& str) {
 25         std::string toReturn(str);
 26         std::transform(toReturn.begin(), toReturn.end(), toReturn.begin(), ::tolower);
 27         return toReturn;
 28     };
 29     /**
 30     * Converts the string \a str to uppercase.
 31     * \param str   String to convert.
 32     * \return Uppercase version of \a str.
 33     */
 34     static std::string uppercase(const std::string& str) {
 35         std::string toReturn(str);
 36         std::transform(toReturn.begin(), toReturn.end(), toReturn.begin(), ::toupper);
 37         return toReturn;
 38     };
 39     /**
 40     * Splits \a str into a vector of strings representing float values.
 41     * Floats formatted as [-][0-9]*.[0-9]* are considered, all other characters in between are ignored.
 42     *
 43     * \param   str     Input string to parse.
 44     *
 45     * \note    TODO: The detection algorithm is a litte simplified and will not yield correct results
 46     *          in every case.
 47     **/
 48     static std::vector<std::string> parseFloats(const std::string& str){
 49          static const std::string floatCharacters("0123456789.-");
 50          std::vector<std::string> toReturn;
 51          size_t strpos = 0;
 52          size_t endpos = 0;
 53          // we just started or just finished parsing an entry, check if finished and skip to beginning of next entry
 54          while ((endpos != std::string::npos) && (strpos = str.find_first_of(floatCharacters, strpos)) != std::string::npos) {
 55              // strpos currently points to the beginning of a float, now find its end
 56              endpos = str.find_first_not_of(floatCharacters, strpos + 1);
 57              // extract float
 58              std::string token = str.substr(strpos, endpos - strpos);
 59              // sanity checks
 60              size_t signPos = token.rfind('-');
 61              if (signPos == 0 || signPos == std::string::npos) { // sign only allowed at beginning
 62                  if (token.find('.'== token.rfind('.')) {      // only one . allowed
 63                      toReturn.push_back(token);
 64                  }
 65              }
 66              strpos = endpos + 1;
 67          }
 68          return toReturn;
 69     };
 70     /**
 71     * Replaces all occurrences of \a from in \a str with \a to.
 72     * \param str   String to perform replacement on.
 73     * \param from  String to be replaced.
 74     * \param to    String replace.
 75     * \return  \a str with all occurrences of \a from replaced with \a to.
 76     */
 77     static std::string replaceAll(const std::string& str, const std::string& from, const std::string& to)
 78          {
 79              std::string toReturn(str);
 80              std::string::size_type strpos = 0;
 81              std::string::size_type foundpos;
 82              while ((foundpos = toReturn.find(from, strpos)) != std::string::npos) {
 83                  toReturn.replace(foundpos, from.size(), to);
 84                  strpos = foundpos + to.size();
 85              }
 86              return toReturn;
 87          };
 88      static  bool replace(std::string& str, const std::string& from, const std::string& to) {
 89          size_t start_pos = str.find(from);
 90          if (start_pos == std::string::npos)
 91              return false;
 92          str.replace(start_pos, from.length(), to);
 93          return true;
 94      };
 95 
 96          static bool replaceLast(std::string& str, const std::string& from, const std::string& to) {
 97          size_t start_pos = str.rfind(from);
 98          if (start_pos == std::string::npos)
 99              return false;
100          str.replace(start_pos, from.length(), to);
101          return true;
102          };
103     /**
104     * Splits the string \a str into pieces separated by the delimiters in \a delimiter.
105     * \param str       String to split.
106     * \param delimiter Set of delimiters.
107     * \return  Vector of the split substrings.
108     */
109     static std::vector<std::string> split(const std::string& line, const std::string& delimiter) {
110          std::vector<std::string> toReturn;
111          std::string::size_type linepos = 0;
112          std::string::size_type endpos = 0;
113          // we are at the beginning of an entry, skip whitespace and check if not already reached end of line
114          while (endpos != std::string::npos) {
115              endpos = line.find_first_of(delimiter, linepos);
116              toReturn.push_back(line.substr(linepos, endpos - linepos));
117              linepos = endpos + 1;
118          }
119          return toReturn;
120      };
121     template<class T>
122     static std::vector<T>  splitStringT(const std::string& line, const std::string& delimiter) {
123          std::vector<T> toReturn;
124          std::string::size_type linepos = 0;
125          std::string::size_type endpos = 0;
126          // we are at the beginning of an entry, skip whitespace and check if not already reached end of line
127          while (endpos != std::string::npos) {
128              endpos = line.find_first_of(delimiter, linepos);
129              toReturn.push_back(StringUtils::fromString<T>(line.substr(linepos, endpos - linepos)));
130              linepos = endpos + 1;
131          }
132          return toReturn;
133      };
134     /**
135     * Trims the string \a str.
136     * All leading and trailing occurrences of the characters in \a whitespace will be removed.
137     *
138     * \param str           The string to trim.
139     * \param whitespace    Set of whitespace characters which shall be removed at the beginning and the end.
140     * \return  The original string without leading and trailing whitespace.
141     */
142     static std::string trim(const std::string& str, const std::string& whitespace = " \t\n\r\0\x0B") {
143         std::string::size_type first = str.find_first_not_of(whitespace);
144         if (first == std::string::npos) {
145             return "";
146         }
147         else {
148             std::string::size_type last = str.find_last_not_of(whitespace);
149             return str.substr(first, last - first + 1);
150         }
151     };
152     /**
153     * Splits the string \a str into trimmed pieces separated by the delimiters in \a delimiter.
154     * Delimiters in quoted strings (\a quotes) will be ignored, double quotes within quoted strings will be
155     * interpreted as literal quotes. Each token will be trimmed.
156     *
157     * \param str           String to split.
158     * \param delimiter     Set of delimiters.
159     * \param quotes        Character used for quotes.
160     * \param whitespace    Set of whitespace characters which shall be removed during trimming.
161     * \return  Vector of the split substrings.
162     */
163      static std::vector<std::string> splitStringsafe(const std::string& str, const std::string& delimiter, char quotes = '"'const std::string& whitespace = " \t\n\r\0\x0B")
164      {
165          std::vector<std::string> toReturn;
166          std::string::size_type strpos = 0;
167          std::string::size_type endpos = 0;
168          // we are at the beginning of an entry, skip whitespace and check if not already reached end of str
169          while ((endpos != std::string::npos) && (strpos = str.find_first_not_of(whitespace, strpos)) != std::string::npos) {
170              // now strpos points to the first non blank character, here starts the entry
171              // check whether there are quotes
172              if (str[strpos] == quotes) {
173                  // find position of closing quotes
174                  endpos = str.find_first_of('"', strpos + 1);
175                  std::string toPush = str.substr(strpos + 1, endpos - strpos - 1);
176                  // ensure we haven't found double quotes ("") which shall be resolved to one double quote in resulting string
177                  while ((endpos != std::string::npos) && (endpos + 1 < str.length()) && (str[endpos + 1== '"')) {
178                      strpos = endpos + 1;
179                      endpos = str.find_first_of('"', endpos + 2);
180                      toPush.append(str.substr(strpos, endpos - strpos));
181                  }
182                  // push string in quotes onto toReturn
183                  toReturn.push_back(trim(toPush, whitespace));
184                  // ignore everything until next delimiter
185                  endpos = str.find_first_of(delimiter, endpos);
186              }
187              // ok, this entry is not in quotes - just push everything until next delimiter onto toReturn
188              else {
189                  endpos = str.find_first_of(delimiter, strpos);
190                  toReturn.push_back(trim(str.substr(strpos, endpos - strpos), whitespace));
191              }
192              strpos = endpos + 1;
193          }
194          return toReturn;
195      };
196     
197     /**
198     * Converts the value \a value to a string.
199     * \param value     The value to convert, must be compatible with std::stringstream.
200     * \return  A string representation of \a value.
201     */
202     template<class T>
203     static std::string toString(const T& value) {
204         std::ostringstream stream;
205         stream << value;
206         return stream.str();
207     };
208     /**
209     * Converts the string \a str to its original value.
210     * \param str   The string to convert
211     * \return  The back-converted value of \a str, type must be compatible with std::stringstream.
212     * \throw   tgt::Exception on conversion failure
213     */
214     template<class T>
215     static T fromString(const std::string& str) {
216         T toReturn;
217         std::istringstream stream;
218         stream.str(str);
219         try
220         {
221             stream >> toReturn;
222         }
223         catch (std::exception& ex)
224         {
225             std::cout << "Failed to convert string '" << str << "'" << "  exception=" << ex.what() << std::endl;
226             return NULL;
227         }
228         return toReturn;
229     };
230     /**
231     * Joins the substrings in \a tokens together using \a delimiter in between.
232     * \param tokens    List of substrings to join.
233     * \param delimiter Delimiter which shall be placed between the substrings.
234     * \return  A string containing the joined substrings.
235     */
236     template<typename T>
237     static std::string join(const std::vector<T>& tokens, const std::string& delimiter) {
238         if (tokens.empty())
239             return "";
240         std::stringstream s;
241         s << tokens[0];
242         for (std::vector<T>::size_type i = 1; i < tokens.size(); ++i)
243             s << delimiter << tokens[i];
244         return s.str();
245     };
246 
247 
248     static bool startsWith(const std::string &str, const std::string &prefix) {
249         return str.size() >= prefix.size() &&
250             str.compare(0, prefix.size(), prefix) == 0;
251     };
252     static bool endsWith(const std::string &str, const std::string &suffix) {
253         return str.size() >= suffix.size() &&
254             str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
255     };
256 
257     static std::string format(const char* format, )//一個參數數量可變的函數  
258     {
259         std::string ret;
260         va_list ap;//新特性
261         va_start(ap, format);
262         char* buf = (char*)malloc(1024 * 100);
263         if (buf != nullptr)
264         {
265             vsnprintf(buf, 1024 * 100, format, ap);
266             ret = buf;
267             free(buf);
268         }
269         va_end(ap);
270         return ret;//返回字符串
271     };
272 }

posted on 2017-05-25 09:59 Benjamin 閱讀(1288) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美视频二区36p| 一本一本久久| 亚洲伊人一本大道中文字幕| 欧美福利专区| 欧美国产乱视频| 国产精品中文字幕欧美| 99在线精品观看| 亚洲人www| 亚洲欧洲视频| 亚洲专区一区| 老司机凹凸av亚洲导航| 久久免费偷拍视频| 欧美freesex8一10精品| 国产精一区二区三区| 国产一区二区三区日韩| 国产主播在线一区| 亚洲激情一区二区| 亚洲永久免费av| 蜜臀av在线播放一区二区三区| 久久久久综合一区二区三区| 亚洲黄色成人| 亚洲美女精品成人在线视频| 亚洲嫩草精品久久| 久久夜色精品国产欧美乱极品| 欧美成人免费全部| 国产欧美日本一区二区三区| 亚洲精品1区2区| 羞羞漫画18久久大片| 欧美成人中文| 亚洲伊人久久综合| 欧美精品一区二区三区久久久竹菊| 国产精品国码视频| 亚洲黄色在线视频| 久久久久久久久久码影片| 亚洲精品少妇| 看欧美日韩国产| 国产精品久久久久永久免费观看| 激情av一区| 欧美一区二区三区免费视频| 亚洲欧洲日韩女同| 乱码第一页成人| 国产欧美日韩一区二区三区在线观看 | 久久婷婷一区| 欧美日韩国产色综合一二三四 | 久久色中文字幕| 久久―日本道色综合久久| 亚洲乱码国产乱码精品精天堂| 久久人人爽国产| 老牛国产精品一区的观看方式| 牛夜精品久久久久久久99黑人| 日韩一本二本av| 欧美福利电影网| 黄色成人av网站| 欧美一区激情| 亚洲欧美国产日韩天堂区| 欧美日韩成人激情| 亚洲毛片播放| 91久久精品视频| 久久久久女教师免费一区| 国产一区二区三区不卡在线观看| 亚洲综合清纯丝袜自拍| 国产精品99久久久久久久久久久久 | 美女视频一区免费观看| 激情六月婷婷久久| 欧美日韩亚洲国产一区| 亚洲精品国产欧美| 最新亚洲电影| 欧美日韩一区二区三区视频| 日韩写真视频在线观看| 亚洲精品一区二区三区四区高清 | 亚洲三级毛片| 久久亚洲一区二区三区四区| 性做久久久久久| 狠狠综合久久av一区二区老牛| 久久精品亚洲一区| 久久本道综合色狠狠五月| 精品成人久久| 欧美激情影院| 欧美日韩精品在线播放| 亚洲免费中文| 久久成人18免费观看| 极品少妇一区二区| 亚洲激情成人网| 欧美亚州一区二区三区| 久久久久久夜精品精品免费| 老司机精品久久| 亚洲欧美怡红院| 欧美一区二区在线播放| 伊人久久大香线蕉av超碰演员| 亚洲丁香婷深爱综合| 欧美偷拍一区二区| 久久亚洲综合| 欧美日韩一区二区国产| 久久久久久久综合| 嫩草成人www欧美| 午夜欧美理论片| 欧美成人免费大片| 欧美制服丝袜| 欧美国产日韩一区二区在线观看| 亚洲欧美精品中文字幕在线| 亚洲肉体裸体xxxx137| 午夜精品理论片| 一区二区三区av| 国产美女搞久久| 免费在线观看成人av| 国产精品黄视频| 亚洲福利精品| 国产精品综合网站| 欧美激情91| 国产区二精品视| 亚洲精品无人区| 亚洲高清一区二| 午夜精品久久久99热福利| 亚洲国产美女精品久久久久∴| 亚洲香蕉伊综合在人在线视看| 亚洲日韩中文字幕在线播放| 久久福利影视| 欧美亚洲在线| 欧美日韩中文字幕精品| 欧美国产视频一区二区| 好吊色欧美一区二区三区四区| 亚洲视频二区| 9人人澡人人爽人人精品| 免费一级欧美片在线播放| 久久久水蜜桃| 国产综合视频| 久久国产精品久久久久久| 亚洲欧美国产三级| 欧美色区777第一页| 国精产品99永久一区一区| 久久一区二区三区四区| 欧美激情一区二区三区全黄| 久久精品视频在线免费观看| 欧美天天影院| 日韩一级在线| av成人激情| 欧美另类99xxxxx| 欧美电影资源| 亚洲国产另类 国产精品国产免费| 亚洲欧美日韩久久精品| 欧美一区2区三区4区公司二百| 欧美日韩精品在线播放| 亚洲乱码国产乱码精品精| 99re热精品| 欧美日韩国产区| 亚洲少妇最新在线视频| 亚洲天堂成人在线视频| 欧美日韩亚洲视频一区| 亚洲精品一区二区三区四区高清| 国产热re99久久6国产精品| 欧美高清一区| 亚洲第一精品福利| 午夜精品久久久久久99热软件| 先锋影音网一区二区| 国产欧美一区二区精品秋霞影院 | 欧美成人一二三| 亚洲卡通欧美制服中文| 欧美精品18+| 亚洲图片在线观看| 欧美在线国产| 亚洲精品久久久久中文字幕欢迎你 | 欧美激情一区二区三区不卡| 中文精品视频一区二区在线观看| 久久精品亚洲精品国产欧美kt∨| 欧美电影免费观看| 亚洲成色最大综合在线| 欧美成人精品一区二区| 亚洲精品乱码久久久久久日本蜜臀| 亚洲免费高清| 国产精品一区二区久久久| 久久蜜桃香蕉精品一区二区三区| 91久久在线播放| 久久国产色av| 99精品视频免费| 国产在线不卡| 欧美日韩国产天堂| 久久国产福利国产秒拍| 亚洲精品资源| 老**午夜毛片一区二区三区| 亚洲午夜av在线| 玉米视频成人免费看| 在线中文字幕日韩| 亚洲大黄网站| 亚洲欧美激情在线视频| 国产精品videossex久久发布| 在线免费观看日本一区| 亚洲综合导航| 狠狠色狠狠色综合日日五| 欧美精品日韩一区| 久久久国产精彩视频美女艺术照福利| 亚洲欧洲综合| 免费观看欧美在线视频的网站| 亚洲欧美国产精品va在线观看| 最新国产乱人伦偷精品免费网站| 国产免费观看久久| 欧美涩涩网站| 欧美国产精品劲爆| 免费欧美日韩国产三级电影| 久久成人资源| 欧美有码在线观看视频|