string获取子串学习

参考资料: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;

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值