//int转字符串
static CString int2Str(int iVal)
{CString sText;sText.Format(_T("%d"), iVal);return sText;
}
//double转字符串,iDot为小数点位数
static CString double2Str(double dVal, int iDot)
{CString sText;sText.Format(_T("%.*f"), iDot, dVal); //iDot替换f前面的*return sText;
}
//用%E格式化浮点数,iDot为小数点位数
static CString double2StrE(double dVal, int iDot)
{CString sText;sText.Format(_T("%.*E"), iDot, dVal);return sText;
}
//判断字符串是否为整数
static bool strIsInt(CString cstr)
{if (cstr.GetLength() < 1) return false;int len = cstr.GetLength();for (int i = 0; i < len; i++){int num = cstr.GetAt(i);if (i == 0 && (num == 43 || num == 45)) continue; //43是加号“+”; 45是减号“-”if (cstr.GetAt(i) >= 48 && cstr.GetAt(i) <= 57) continue;else return false;}return true;
}
//判断字符串是否为浮点数
static bool strIsDouble(CString cstr)
{if (cstr.GetLength() < 1) return false;int decimal_num = 0;int len = cstr.GetLength();for (int i = 0; i < len; i++){int num = cstr.GetAt(i);if (i == 0 && (num == 43 || num == 45)) continue; //43是加号“+”; 45是减号“-”if (cstr.GetAt(i) >= 48 && cstr.GetAt(i) <= 57) continue;else if (cstr.GetAt(i) == 46) decimal_num++;else return false;}if (decimal_num <= 1) return true;else return false;return true;
}
//判断字符串是否为字母和数字的组合
static bool strIsalnum(CString cstr)
{if (cstr.GetLength() < 1) return false;int len = cstr.GetLength();for (int i = 0; i < len; i++){int num = cstr.GetAt(i);if ((num >= 'A' && num <= 'Z') ||(num >= 'a' && num <= 'z') ||(num >= '0' && num <= '9') ||num == '.'){;}elsereturn false;}return true;
}
//判断字符串是否包含中文
static bool strContainGB(const CString &cstr)
{if (cstr.GetLength() < 1) return false;int len = cstr.GetLength();for (int i = 0; i < len; i++){int num = cstr.GetAt(i);if (num > 123)return true;}return false;
}//获取字符串最左边的纯字母字符串
static CString getLeftLetterStr(CString cstr)
{int nNumStr = 0;int len = cstr.GetLength();for (int i = 0; i < len; i++){int num = cstr.GetAt(i);if ((num >= 'A' && num <= 'Z') || (num >= 'a' && num <= 'z')){nNumStr++;continue;}elsebreak;}CString str = cstr.Left(nNumStr);return str;
}
//获取字符串最左边的数字串
static CString getLeftNumStr(CString cstr)
{int nNumStr = 0;int len = cstr.GetLength();for (int i = 0; i < len; i++){int num = cstr.GetAt(i);if (num >= '0' && num <= '9'){nNumStr++;continue;}elsebreak;}CString str = cstr.Left(nNumStr);return str;
}
//获取字符串最右边的数字串
static CString getRightNumStr(CString cstr)
{int nNumStr = 0;int len = cstr.GetLength();for (int i = len - 1; i > -1; i--){int num = cstr.GetAt(i);if (num >= '0' && num <= '9'){nNumStr++;continue;}elsebreak;}CString str = cstr.Right(nNumStr);return str;
}
//获取字符串中第一个数字串
static CString getFirstNumStr(CString cstr)
{int iFirst = -1;int nCount = 0;int len = cstr.GetLength();for (int i = 0; i < len; i++){int num = cstr.GetAt(i);if (num >= '0' && num <= '9'){if (iFirst == -1) //数字开始{iFirst = i;}nCount++;}else{if (iFirst != -1) //数字结束{break;}nCount = 0;}}CString str = cstr.Mid(iFirst, nCount);return str;
}//nPrecision控制小数点位数,采用抹去法,如果要四舍五入需要自己处理好了再送进来。static CString DoubleToCString(double dValue, int nPrecision = 2){CString cstr("");if (nPrecision <= 0){cstr.Format(L"%.0f", dValue);}else{CString strFormat;strFormat.Format(_T("%%0.%df"), nPrecision);cstr.Format(strFormat, dValue);}return cstr;
}static CString IntToCString(int nValue){CString cstr("");cstr.Format(L"%d", nValue);return cstr;
}static double CStringToDouble(CString cstr){return _tstof(cstr);
}static int CStringToInt(CString cstr){int nRe(0);nRe = _tstoi(cstr);return nRe;
}//拆开字符串,某个字符为标记。标记前的输出为cstr1,标记后的部分输出为cstr2,标记扔了掉。static void DevideCstring(const CString& cstr, char chSign, CString& cstr1, CString& cstr2){int nSign = cstr.Find(chSign);if (nSign > 0 ){cstr1 = cstr.Left(nSign); //从左数多少个int nLength = cstr.GetLength();cstr2 = cstr.Right(nLength - (nSign + 1)); //从右数多少个}else{cstr1 = cstr;}
}//拆开字符串,某个字符为标记。去除标记后,按顺序存入vecstatic void DevideCstring2(const CString& cstr, char chSign, vector& vecCstr){CString cstr1, cstr2;StTool::DevideCstring(cstr, chSign, cstr1, cstr2);vecCstr.push_back(cstr1);int nSign = cstr.Find(chSign);if (nSign > 0) //如果还有{DevideCstring2(cstr2, chSign, vecCstr);}
}static std::string CString2String(CString strC){std::string strStd;
#ifndef _UNICODEstrStd = std::string((LPCTSTR)strC); //不是UNICODE,全部都是窄字节,直接转换到char* 再到string。
#endif
#ifdef _UNICODECT2CA pszConvertedAnsiString(strC); //CW2AstrStd = pszConvertedAnsiString;
#endifreturn strStd;
}//判断CString字符串是否纯数字static bool IsCStrAllNumber(const CString& cstr)bool StTool::IsCStrAllNumber(const CString& cstr)
{return cstr == cstr.SpanIncluding(_T("0123456789"));
}//判断CString字符串是否纯中文static bool IsCStrAllChinese(const CString& cstr){bool bRet(true);#ifdef _UNICODE int nLen1 = (int)cstr.GetLength();for (int i = 0; i != nLen1; ++i){int unic = (int)cstr.GetAt(i);if (unic <= 255){bRet = false;break;}else{continue;}}#else int nLen2 = (int)strlen(cstr);unsigned char ansi;for (int i = 0; i < nLen2; i++){ansi = cstr[i];if (ansi <= 127){bRet = false;break;}else{continue;}}#endifreturn bRet;
}//取得CString字符串中文、英文字符数量static void GetCStrChineseAndEnglishNum(const CString& cstr, int& nCh, int& nEn){#ifdef _UNICODECString shuzi, biaodianfuhao, hanzi, daxiezimu, xiaoxiezimu;for (int i = 0; i < cstr.GetLength(); i++){int unicode = (int)cstr.GetAt(i);if (unicode <= '9' && unicode >= '0'){shuzi += cstr.GetAt(i);}else if (unicode <= 'z' && unicode >= 'a'){xiaoxiezimu += cstr.GetAt(i);}else if (unicode <= 'Z' && unicode >= 'A'){daxiezimu += cstr.GetAt(i);}else if (unicode > 255){hanzi += cstr.GetAt(i);}else{biaodianfuhao += cstr.GetAt(i);}}nCh = hanzi.GetLength();nEn = shuzi.GetLength() + biaodianfuhao.GetLength() + daxiezimu.GetLength() + xiaoxiezimu.GetLength();#elseunsigned char ansi;int lens = (int)strlen(cstr);for (int i = 0; i < lens; i++){ansi = cstr[i];if (ansi > 127){nCh++;}else if ((ansi <= '9' && ansi >= '0') || (ansi <= 'z' && ansi >= 'a') || (ansi <= 'Z' && ansi >= 'A')){nEn++;} } #endif
}//取得CString字符串中阿拉伯数字个数static void GetCStrNumberSize(const CString& cstr, int& nNum){
#ifdef _UNICODECString shuzi;for (int i = 0; i < cstr.GetLength(); i++){int unicode = (int)cstr.GetAt(i);if (unicode <= '9' && unicode >= '0'){shuzi += cstr.GetAt(i);}}nNum = shuzi.GetLength();#elseunsigned char ansi;int lens = (int)strlen(cstr);for (int i = 0; i < lens; i++){ansi = cstr[i];if (ansi <= '9' && ansi >= '0'){nNum++;}}#endif
}//获取字符串的平均个数,用来计算文字长度。长度 = 平均个数 * 文字宽度static int GetCStringLength(CString cstr){int nRe(0);int nChinese(0), nEnglish(0);GetCStrChineseAndEnglishNum(cstr, nChinese, nEnglish);nRe = nChinese * 1.4 + 1.05 * nEnglish; //系数如果不合适可以调整。return nRe;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!