1.比较字符串
strcmp(testStr, "no") == 0
2. 字符串大小写
char * strlwr(char * s)将s 中的字母都变成小写
char * strupr(char * s)将s 中的字母都变成大写
string testStr = “just For test”;
transform(testStr.begin(),testStr.end(),testStr.begin(),::tolower);
3. CString和char[]互转
只有以NULL结尾的char[]才能强制转换为CString,即可以直接等于,否则需要通过Format函数
char charArray[]="this C++";
CString res;
res.Format("%s", charArray);
4. CString和string互转
CString转string
CString strMfc = "test";
std::string strStr = strMfc.GetBuffer(0);
string转CString
CString strMfc;
string strStr = "test";
strMfc = strStr.c_str();
5. string和char数组
string转char数组
char buf[10];
string str("ABCDEFG");
length = str.copy(buf, 9);
buf[length] = '\0';
或者
strcpy(buf, str.c_str());
strncpy(buf, str.c_str(), 10);
char数组转string
char ch [] = "ABCDEFG";
string str(ch); 也可string str = ch;
或者
char ch [] = "ABCDEFG";
string str;
str = ch;//在原有基础上添加可以用str += ch;
6. 填充/提取
char timeInfo[256];
memset(timeInfo, 0, sizeof(timeInfo));
sprintf(timeInfo, "%04d/%02d/%02d %02d:%02d:%02d ", year,mon,day, hour, min, sec);
1)sscanf("zhoue3456 ", "%4s", str); //取指定长度的字符串
printf("str=%s\n", str); //str="zhou";
2)sscanf("zhou456 hedf", "%[^ ]", str); //取到指定字符为止的字符串,取遇到空格为止字符串
printf("str=%s\n", str); //str=zhou456;
3)sscanf("654321abcdedfABCDEF", "%[1-9a-z]", str); //取仅包含指定字符集的字符串
printf("str=%s\n", str); //str=654321abcded,只取数字和小写字符
4)sscanf("BCDEF123456abcdedf", "%[^a-z]", str); //取到指定字符集为止的字符串
printf("str=%s\n", str); // str=BCDEF123456, 取遇到大写字母为止的字符串
5)int a,b,c;
sscanf("2015.04.05", "%d.%d.%d", &a,&b,&c); //取需要的字符串
printf("a=%d,b=%d,c=%d",a,b,c); // a=2015,b=4,c=5
截取函数实现
void GetSubStrings(const string& totalStr, const string& delim, vector<string>& res)
{
res.clear();
if (totalStr == "")
{
return ;
}
char *strArray = new char[totalStr.length() + 1];
strcpy(strArray, totalStr.c_str());
char *delimArray = new char[delim.length() + 1];
strcpy(delimArray, delim.c_str());
char* curChar = strtok(strArray, delimArray);
while (curChar)
{
string curStr = curChar;
res.push_back(curStr);
curChar = strtok(NULL, delimArray);
}
}
标签:ch,string,CString,char,str,相关,字符串,操作 From: https://www.cnblogs.com/xiaoxiaoshi/p/17483478.html