|
Apr
22
|
|
又做了一件无聊的事,把php里的字符串操作函数用c++写了一遍,备不时之需。
其中与HTML相关的函数,加密相关函数没有写。
另外,这些函数只是实现了功能,算法和效率上未经考究。
字符串的其他操作请见:C++标准库string类型的操作总结
其中包括的Array.h库在这里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | #ifndef _STR_H_ #define _STR_H_ #include <string> #include <cmath> #include <cctype> //#include <algorithm> #include "Array.h" #ifndef sint #define sint string::size_type #endif #ifndef ints #define ints Array<int,int> #endif #ifndef strings #define strings Array<int,string> #endif using namespace std; /***************** * string addslashes ( string str ) * * 返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。 * 这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。 * / string addslashes(const string &str){ string temp = ""; for(sint i=0; i!=str.size(); i++){ if(str[i]==chr(39) || str[i]==chr(34) || str[i]==chr(92) || str[i]==chr(0)){ temp += '\\' ; temp += str[i]; } else temp += str[i]; } return temp; } */ /***************** * string rtrim ( string str [, string charlist] ) * * This function returns a string with whitespace stripped from the end of str. * Without the second parameter, rtrim() will strip these characters: * " " (ASCII 32 (0x20)), an ordinary space. * "\t" (ASCII 9 (0x09)), a tab. * "\n" (ASCII 10 (0x0A)), a new line (line feed). * "\r" (ASCII 13 (0x0D)), a carriage return. * "\0" (ASCII 0 (0x00)), the NUL-byte. */ string rtrim(const string &str, const string &charlist = " \t\n\r\0"){ string s = str; for(sint i=0; i!=charlist.size(); i++){ if( s.rfind(charlist[i])==s.size()-1 ){ s=rtrim(s.substr(0,s.size()-1)); } } return s; } /*********** * string chr ( int ascii ) * * 返回相对应于 ascii 所指定的单个字符。 */ char chr(const int &asc){ return (char)asc; } /*********** * int ord ( string string ) * * Returns the ASCII value of the first character of string. */ int ord(const char &c){ return (int)c; } /*********** * return the ascii of the first char of str */ int ord(const string &str){ return (int)str[0]; } /*********** * int asc(string string) * This is an alias of ord */ int asc(const char &c){ return ord(c); } int asc(const string &str){ return ord(str); } /************ * mixed count_chars ( string string ) * * 统计 string 中每个字节值(0..255)出现的次数。 */ ints count_chars(const string &str){ ints is(256,0); for(sint i=0; i!=str.size(); i++){ is[ord(str[i])] ++ ; } return is; } /************* * array explode ( string separator, string string [, int limit] ) * * 此函数返回由字符串组成的数组,每个元素都是 string 的一个子串, * 它们被字符串 separator 作为边界点分割出来。 */ strings explode(const string &separator, const string &str){ string s = str; strings ss; sint pos; do{ pos = s.find(separator); ss += make_pair(ss.size(),string(s,0,s.find_first_of(separator))); s = s.substr(pos+separator.size()); } while ( pos!=string::npos ); return ss; } strings split(const string &separator, const string &str){ return explode(separator, str); } strings str_split(const string &separator, const string &str){ return explode(separator, str); } /************** * string implode ( string glue, array pieces ) * * Returns a string containing a string representation of all the array * elements in the same order, with the glue string between each element. * */ string implode(const string &glue, strings &ss){ string s = ss[0] ; for(sint i=1; i!=ss.size(); i++){ s += glue + ss[i]; } return s; } string join(const string &glue, strings &ss){ return implode(glue,ss); } /***************** * string ltrim ( string str [, string charlist] ) * * This function returns a string with whitespace stripped from the beginning of str. * Without the second parameter, rtrim() will strip these characters: * " " (ASCII 32 (0x20)), an ordinary space. * "\t" (ASCII 9 (0x09)), a tab. * "\n" (ASCII 10 (0x0A)), a new line (line feed). * "\r" (ASCII 13 (0x0D)), a carriage return. * "\0" (ASCII 0 (0x00)), the NUL-byte. * "\x0B" (ASCII 11 (0x0B)), a vertical tab. */ string ltrim(const string &str, const string &charlist=" \t\n\r\0"){ string s = str; for(sint i=0; i!=charlist.size(); i++){ if(s.find(charlist[i])==0){s=ltrim(s.substr(1));} } return s; } //trim a string string trim(const string& str){ string s=str; //trim left s = ltrim(str); //trim right s = rtrim(s); return s; } /************** * string str_replace(string search, string replace, string subject[ , int &count]) * * This function returns a string with all occurrences of search * in subject replaced with the given replace value. * The amount of occurrences will be stored in count if given */ string str_replace(const string &search, const string &replace, const string &subject, int &count){ strings ss = split(search, subject); count = ss.size() - 1; return join(replace, ss); } string str_replace(const string &search, const string &replace, const string &subject){ strings ss = split(search, subject); return join(replace, ss); } /*************** * string strtolower(string string) * * Make a string lowercase */ string strtolower(const string &str){ string s = ""; for(sint i=0; i!=str.size(); i++){ s += tolower(str[i]); } return s; } /*************** * string strtoupper(string string) * * Make a string uppercase */ string strtoupper(const string &str){ string s = ""; for(sint i=0; i!=str.size(); i++){ s += toupper(str[i]); } return s; } /************** * string str_ireplace(string search, string replace, string subject[ , int &count]) * * Case-insensitive str_replace */ string str_ireplace(const string &search, const string &replace, const string &subject, int &count){ ints is(subject.size(),0); for(sint i=0; i!=subject.size(); i++){ if(isupper(subject[i])) is[i] = 1; } string str = strtolower(subject); string srh = strtolower(search); strings ss = split(srh, str); count = ss.size() - 1; strings rs(ss.size(),string("")); sint pos = 0; for(sint i=0; i!=ss.size(); i++){ for(sint j=0; j!=ss[i].size(); j++){ if(is[pos]) rs[i] = strtoupper(ss[i]); else rs[i] = ss[i]; pos++; } pos += search.size(); } return join(replace, rs); } string str_ireplace(const string &search, const string &replace, const string &subject){ ints is(subject.size(),0); for(int i=0; i!=subject.size(); i++){ if(isupper(subject[i])) is[i] = 1; } string str = strtolower(subject); string srh = strtolower(search); strings ss = split(srh, str); strings rs(ss.size(),string("")); sint pos = 0; for(int i=0; i!=ss.size(); i++){ for(sint j=0; j!=ss[i].size(); j++){ if(is[pos]) rs[i] = strtoupper(ss[i]); else rs[i] = ss[i]; pos++; } pos += search.size(); } return join(replace, rs); } /*************** * string str_repeat ( string input, int multiplier ) * * Returns input_str repeated multiplier times. * multiplier has to be greater than or equal to 0. * If the multiplier is set to 0, the function will return an empty string. */ string str_repeat(const string &input, const int &mult){ string s = ""; for(int i=0; i<mult; i++){ s += input; } return s; } /*************** * string str_shuffle ( string str ) * * str_shuffle() shuffles a string. */ string str_shuffle(const string &str){ string s = ""; ints subscripts; for(sint i=0; i!=str.size(); i++) subscripts[i] = i; while(!subscripts.empty()){ srand(time(NULL)); int r = rand() % subscripts.size(); s += str[subscripts[r]]; subscripts.erase(subscripts[r]); } return s; } /*************** * int str_word_count(string string) * * Counts the number of words inside string. */ int str_word_count(const string &str){ strings ss = split(" ", str); int count = 0; for(sint i=0; i!=ss.size(); i++){ if(!ss[i].empty()) count++; } return count; } /**************** * string strstr ( string haystack, string needle ) * * Returns part of haystack string from the first occurrence of needle to the end of haystack. */ string strstr(const string &haystack, const string &needle){ return haystack.substr(haystack.find_first_of(needle)); } string strchr(const string &haystack, const string &needle){ return strstr(haystack, needle); } /***************** * int stripos ( string haystack, string needle) * * stripos -- Find position of first occurrence of a case-insensitive string */ sint stripos(const string &haystack, const string &needle){ return strtolower(haystack).find_first_of(strtolower(needle)); } /***************** * int strlen ( string string )\ * * Returns the length of the given string. */ sint strlen(const string &str){ return str.size(); } /***************** * int strpos ( string haystack, string needle) * * strpos -- Find position of first occurrence of a string */ sint strpos(const string &haystack, const string &needle){ return haystack.find_first_of(needle); } /***************** * int strrchr ( string haystack, string needle) * * strrchr -- Find position of last occurrence of a string */ sint strrchr(const string &haystack, const string &needle){ return haystack.find_last_of(needle); } /***************** * string strrev ( string string ) * * Returns string, reversed. */ string strrev(const string &str){ string s = ""; for(int i=str.size()-1; i>=0; i--){ // type of i here must be int, not sint(string::size_type), // because the last i is -1 s += str[i]; } return s; } /***************** * int strripos ( string haystack, string needle) * * strripos -- Find position of last occurrence of a case-insensitive string */ sint strripos(const string &haystack, const string &needle){ return strtolower(haystack).find_last_of(strtolower(needle)); } /***************** * int strrpos ( string haystack, string needle) * * strrpos -- Find position of last occurrence of a string */ sint strrpos(const string &haystack, const string &needle){ return haystack.find_last_of(needle); } //for int string num2str(const int &num, const string &format = "%d"){ /* int temp = num; string str = ""; string flag; if( temp==0 ) return "0"; flag = temp<0 ? "-" : ""; temp = abs(temp); while( temp!=0 ){ str = char( int(temp % 10)+48 ) + str; temp = (int)temp/10; } return flag+str; */ char s[32]; sprintf(s, format.c_str(), num); return string(s); } // for float string num2str(const double &num, const string &format = "%f"){ /* int ipart = (int)num; double fpart = num - (double)ipart; cout << fpart << endl; if( ipart<0 ) fpart = -fpart; string s = num2str(ipart); s += "."; s += num2str((int)(fpart*pow(10,(double)fracdigital))); return s; */ char s[32]; sprintf(s, format.c_str(), num); return string(s); } /************* * string left(string str, int n) * * return a string containing left n chars of str */ string left(const string &str, const int &n){ return str.substr(0,n); } /************* * string right(string str, int n) * * return a string containing right n chars of str */ string right(const string &str, const int &n){ return str.substr(str.size()-n); } /************* * string substr(string str, int pos[, int n]) * * return a string containing n chars from pos of str */ string substr(const string &str, const int &pos, const unsigned &n){ int p = pos, s = str.size(); if( p<=-s || p>=s ){ cerr << "Warning: substr: invalid parameter pos " << p << ", str.size() = " << s << endl; return ""; } if( p<0 ) p += s; return str.substr(p,n); } string substr(const string &str, const int &pos){ return substr(str,pos,str.size()-pos); } string mid(const string &str, const int &pos, const unsigned &n){ return substr(str,pos,n); } /************* * double str2num(const string &str) * * convert a string to a double */ double str2num(const string &str){ strings ss = split(".",str); bool flag = true; double s = 0.; if(ss.size()>2){ cerr << "Warning: str2num: too many points in " << str << ", preserve the first one." << endl; for(sint i=2; i<ss.size(); i++) ss[1] += ss[i]; } if( left(ss[0],1)=="-" ){ flag = false; ss[0] = ss[0].substr(1); } else if( left(ss[0],1)=="+" ){ ss[0] = ss[0].substr(1); } string ss0 = ""; for(string::size_type i=0; i<ss[0].size(); i++){ if(isdigit(ss[0][i])) ss0 += ss[0][i]; } for(sint i=0; i<ss0.size(); i++){ if(isdigit(ss0[i])) s += ((int)ss0[i]-48)*pow(10.,(double)(ss0.size()-i-1)); } if(ss.size()>1){ int n = 0; for(sint i=0; i<ss[1].size(); i++){ if(isdigit(ss[1][i])) s += ((int)ss[1][i]-48)*pow(10.,(double)(-1-n)); else n--; n++; } } return flag?s:-s; } #endif |
这篇文章来自 迷途知返(PWWANG.COM), 转载请注明出处。 版权说明


还是习惯用String.h的,现在开始怀念当年自己用C写的字符操作包的经历了
String.h有好用的函数吗?上面这些用泛型算法应该也能实现,无奈泛型算法学得不好,看来大家都有这种写操作包的习惯
在非MFC环境下用string还是非常不错的,很多操作也都封装好了.
泛型编程我也是头大的,不过也没什么时间研究了,最近就看看STL与DIRECTX