参考资料:http://www.cnblogs.com/wangkangluo1/archive/2011/07/22/2114118.html
用到函数如下
string类的查找函数:
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
string的子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
string的连接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾
说明:用c++实现。代码支持MultiByte、Unicode两种编码方式。
目的:
将“/include library/object/0/null/q/twq/100336/0cec97ce-d646-4f75-add7-9ea9195777de”
转化为“/0/0cec97ce-d646-4f75-add7-9ea9195777de”
实现:
#include "stdafx.h"
#include <string>
using namespace std;
#ifdef UNICODE
typedef std::wstring LBIMString;
#else
typedef std::string LBIMString;
#endif
#ifdef UNICODE
typedef wchar_t LBIMChar;
#else
typedef char LBIMChar;
#endif
//字符串转化
void StringConvert();
int _tmain(int argc, _TCHAR* argv[])
{
StringConvert();
return 0;
}
void StringConvert()
{
#ifdef UNICODE
LBIMString strSource = L"/include library/object/0/null/q/twq/100336/0cec97ce-d646-4f75-add7-9ea9195777de";
const LBIMChar cFind = L'/';
#else
LBIMString strSource = "/include library/object/0/null/q/twq/100336/0cec97ce-d646-4f75-add7-9ea9195777de";
const LBIMChar cFind = '/';
#endif
//解析出floor
size_t nStPos = 0;
size_t nEdPos = 0;
size_t nTemp = 0;
for (int i = 0; i <= 3; i++)
{
nTemp = strSource.find(cFind, nTemp);
if (LBIMString::npos == nStPos)
{
break;
}
switch (i)
{
case 2: nStPos = nTemp; break;
case 3: nEdPos = nTemp; break;
}
nTemp += sizeof(LBIMChar);
}
if ( 0 == nStPos
|| 0 == nEdPos)
{
return;
}
LBIMString strFloor = strSource.substr(nStPos, nEdPos - nStPos);
//解析出guid
size_t nRevPos = 0;
nRevPos = strSource.rfind(cFind);
LBIMString strEntity = strSource.substr(nRevPos, strSource.size() - nRevPos);
#ifdef UNICODE
LBIMString strDesc = L"";
#else
LBIMString strDesc = "";
#endif
strDesc += strFloor;
strDesc += strEntity;
}