//Header File(.h)
#pragma once
// CSplitStr
class CSplitStr
{
private:
//切分的标志符号
CString m_sSplitFlag;
//连续的切分的标志符号当成一个标志处理
BOOL m_bSequenceAsOne;
//被切分的文本
CString m_sData;
public:
CSplitStr();
virtual ~CSplitStr();
//得到切分好的文本串
void GetSplitStrArray(CStringArray &array);
//得到被切分的文本
CString GetData();
//设置被切分的文本
void SetData(CString sData);
//得到切分参数
BOOL GetSequenceAsOne() {return m_bSequenceAsOne;};
//设置切分参数
void SetSequenceAsOne(BOOL bSequenceAsOne) {m_bSequenceAsOne = bSequenceAsOne;};
//得到切分标志
CString GetSplitFlag() {return m_sSplitFlag;};
//设置切分标志
void SetSplitFlag(CString sSplitFlag) {m_sSplitFlag = sSplitFlag;};
};
//Source File(.cpp)
// SplitStr.cpp : 实现文件
//
#include "stdafx.h"
#include "SplitStr.h"
// CSplitStr
CSplitStr::CSplitStr()
{
SetData("");
SetSequenceAsOne(TRUE);
SetSplitFlag(",");
}
CSplitStr::~CSplitStr()
{
}
void CSplitStr::SetData( CString sData )
{
m_sData = sData;
m_sData.TrimLeft();
m_sData.TrimRight();
}
CString CSplitStr::GetData()
{
return m_sData;
}
void CSplitStr::GetSplitStrArray( CStringArray &array )
{
CString sData = GetData();
CString sSplitFlag = GetSplitFlag();
if (sData.Right(1) != sSplitFlag) sData +=sSplitFlag;
CString sTemp;
int pos =-1;
while ((pos=sData.Find(sSplitFlag,0)) != -1)
{
sTemp = sData.Left(pos);
if (!GetSequenceAsOne())
{
array.Add(sTemp);
}
else
{
if (!sTemp.IsEmpty() && sTemp !="") ////连续的分隔符视为单个处理
{
array.Add(sTemp);
}
}
sData = sData.Right(sData.GetLength() - pos - 1);
}
}
//Useage Example
CSplitStr Split;
CString m_sText = "Hello,World";
Split.SetSplitFlag(",");
Split.SetSequenceAsOne(TRUE);
Split.SetData(m_sText);
CStringArray array;
Split.GetSplitStrArray(array);
int i = array.GetCount();
CString str = array[i-1];
//str == "World";