您的位置:首页 > 其它

关于字符串操作常用的一些总结

2014-07-27 14:33 609 查看
很多时候都会面对各种字符串转换和截取等等问题:写了一些常用的方法共分享:

/************************************************

  函数名称:ByteToWChar

  函数功能:窄字节转宽字节

  输入参数:
szByte  窄字节字符串
wstr    输出宽字节

  输出参数:

   void

*************************************************/

void CKpString::ByteToWChar(LPCSTR szByte, std::wstring& wstr)

{
//预转换,得到所需空间的大小
int nstrlen = strlen(szByte);
int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, szByte, nstrlen, NULL, 0);
wchar_t* pWChar = new wchar_t[wcsLen+1];
memset(pWChar, 0, (wcsLen+1)*sizeof(wchar_t));
::MultiByteToWideChar(CP_ACP, NULL, szByte, nstrlen, pWChar, wcsLen);
wstr = pWChar;
delete[] pWChar;

}

/************************************************

  函数名称:WCharToByte

  函数功能:宽字节转窄字节

  输入参数:
szWChar 宽字节字符串
strByte 输出窄字节字符串

  输出参数:

   void

*************************************************/

void CKpString::WCharToByte(wchar_t* szWChar, std::string& strByte)

{
//预转换,得到所需空间的大小
DWORD dwszlen = WideCharToMultiByte(CP_OEMCP,NULL,szWChar,-1,NULL,0,NULL,FALSE);
char *pszText= new char[dwszlen];
if(!pszText){
delete []pszText;return;
}
WideCharToMultiByte (CP_OEMCP,NULL,szWChar,-1,pszText,dwszlen,NULL,FALSE);
strByte=pszText;
delete []pszText;

}

/************************************************

  函数名称:WCharToUTF8

  函数功能:GB2132扩展码转UTF_8

  输入参数:
strGBK 输入字符串

  输出参数:

   string UTF8

*************************************************/

string CKpString::WCharToUTF8(const std::string& strGBK)

{
string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
str1 = new WCHAR
;
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
char * str2 = new char
;
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
strOutUTF8 = str2;
delete[]str1;
str1 = NULL;
delete[]str2;
str2 = NULL;
return strOutUTF8;

}

/************************************************

  函数名称:UTF8ToWChar

  函数功能:UTF_8转GB2132扩展码

  输入参数:
strUTF8 输入UTF_8字符串

  输出参数:

   string 

*************************************************/

string CKpString::UTF8ToWChar( const std::string& strUTF8 )

{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
wchar_t * wszGBK = new wchar_t[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUTF8.c_str(), -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;

}

/************************************************

  函数名称:Split

  函数功能:分割字符串存到vecctor

  输入参数:
pszStr 要分割的字符串
pszMark 分割符
vecStr  要存入数据的容器

  输出参数:

   BOOL

*************************************************/

BOOL CKpString::Split(LPCSTR pszStr,LPCSTR pszMark,vector<string>& vecStr)

{
string strCode=pszStr;
while (TRUE){
if (strCode.find(pszMark)==string::npos){
if (strCode.length()>0){
vecStr.push_back(strCode.c_str());
}
break;
<
4000
/span>}
string strTmp=strCode.substr(0,strCode.find(pszMark));
vecStr.push_back(strTmp.c_str());
if (strCode.length()>0){
strCode=strCode.substr(strCode.find(pszMark)+1);
}
}
return TRUE;

}

/************************************************

  函数名称:FtoA

  函数功能:浮点数转换为字符串 返回字符

  输入参数:
douData 浮点数
nCount 要保留的小数位数
buff   输出字符串

  输出参数:

   输出字符串

*************************************************/

LPSTR CKpString::FtoA(double douData, int nCount, LPSTR buff) 

{
int nLeft=(int)douData;
int nBianhua=10;
int add=10;
int nWhileCount=0;
while (nLeft==0)
{
++nWhileCount;
nLeft=(int)(douData*add);
add*=10;
}
for (int i=0;i<(nCount-1);i++)
{
nBianhua*=10;
}
nLeft=(int)(douData*nBianhua);
memset(buff,0,sizeof(buff));
sprintf(buff,"%d",nLeft);
string strTemp="";
while (nWhileCount>0) 
{
--nWhileCount;
strTemp+="0";
}
strTemp+=buff;
strTemp=strTemp.insert(strTemp.length()-nCount,".");
memset(buff,0,sizeof(buff));
sprintf(buff,"%s",strTemp.c_str());
return buff;

}

/************************************************

  函数名称:FtoAe

  函数功能:浮点数转换为字符串 返回字符 科学计数法表示
douData 浮点数
nCount 要保留的小数位数
buff   输出字符串

   输出参数:
     输出字符串

*************************************************/

LPSTR CKpString::FtoAe(double douData, int nCount, LPSTR buff)

{
char* buf = _gcvt(douData, 5,buff);
return buf;

}

/************************************************

  函数名称:GetCharCount

  函数功能:获取字符串字符个数,输出汉字个数,返回字符总数

  输入参数:
pszBuf 输入字符串
nChineseChar 输出汉字个数

  输出参数:

   int 字符总数

*************************************************/

int CKpString::GetCharCount(LPSTR pszBuf,int& nChineseChar)

{ //判断ASCLL码
int nChineseChars=0,nChars=0;
for (int j=0;j<strlen(pszBuf);j++){
int nChar=pszBuf[j];
if (nChar<0){//编码大于0为汉字编码
nChineseChars++;
}else{
nChars++;
}
}
nChineseChar=nChineseChars/2;
return nChars+nChineseChar;

}

/************************************************

  函数名称:AtoF

  函数功能:字符转浮点数

  输入参数:
pszBuf 浮点数样式的字符串

  输出参数:

   double

*************************************************/

double CKpString::AtoF(LPSTR pszBuf)

{
return atof(pszBuf);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vc 扩展 编码 string 数据