RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
C++ string的几个函数
  • 作者:zhaozj
  • 发表时间:2020-12-23 10:38
  • 来源:未知

int StrLen(const char* str){const char *sc;

for (sc = str; *sc != '/0'; ++sc);

return sc - str;}

char* StrTrim(char* s){char* pDst = s;

while(1){if (*pDst == ' ' || *pDst == '/t' || *pDst == '/n'){pDst++;}else{break;}}

char* p = pDst;while(*p) { p++; }p--;while(1){if (*p == ' ' || *p == '/t' || *p == '/n'){p--;}else{break;}}*(p+1) = '/0';

return pDst;}

char* StrCat(char* dst, const char* src){char *pTemp = dst;

while (*pTemp) pTemp++;

while (*pTemp++ = *src++);

return dst;}

int StrCmp(const char * dst, const char * src){int ret = 0 ;while (!(ret = *dst - *src) && *dst){++src;++dst;}

if (ret < 0 )ret = -1 ;else if (ret > 0 )ret = 1 ;

return ret;}

int StrCmp(const char * dst, const char * src, int count){int ret = 0 ;while (!(ret = *dst - *src) && *dst){if (--count == 0)break;++src;++dst;}

if (ret < 0 )ret = -1 ;else if (ret > 0 )ret = 1 ;

return ret;}

char* StrCpy(char * dst, const char * src){char* pTemp = dst;

while (*pTemp++ = *src++);

return dst;}

char* StrCpy(char * dst, const char * src, int count){char* pTemp = dst;

while (count-- && (*pTemp++ = *src++));

return dst;}

char * StrChr(const char * s, int c){for(; *s != (char) c; ++s) if (*s == '/0') return NULL;return (char *) s;}

char * StrRChr(const char * s, int c){ const char *p = s + StrLen(s); do  { if (*p == (char)c) return (char *)p; } while (--p >= s);

 return NULL;}

char * StrStr(const char * s1,const char * s2){int l1, l2;

l2 = StrLen(s2);if (!l2)return (char *) s1;l1 = StrLen(s1);while (l1 >= l2) {l1--;if (!StrCmp(s1,s2,l2))return (char *) s1;s1++;}return NULL;}

int StrSpn(const char *s, const char *accept){const char *p;const char *a;int count = 0;

for (p = s; *p != '/0'; ++p) {for (a = accept; *a != '/0'; ++a) {if (*p == *a)break;}if (*a == '/0')return count;++count;}

return count;}