分割字符串(CString)
来源:互联网 发布:tensorflow pdf下载 编辑:程序博客网 时间:2024/06/01 20:34
分割字符串(CString)2010-05-20 13:38// Test3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Test3.h"
#include <tchar.h>
#define _UNICODE
#define UNICODE
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
//最后一个参数返回的是子字符串的数量
CString * SplitString(CString str, char split, int& iSubStrs);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
//cout << (LPCTSTR)strHello << endl;
}
CString str = "sssdf sfds jieuri lj122 98098";
// CString str = "sfsfsf";
CString* pStr;
int iSubStrs;
pStr = SplitString(str, ' ', iSubStrs);
//如果子字符串的数量为1
if (iSubStrs == 1)
{
//Convert CString to char
char* pCh = (LPSTR)(LPCTSTR)str;
printf("%s\n", pCh);
}
else
{
//输出所有子字符串
for (int i = 0; i < iSubStrs; i++)
{
//Convert CString to char
char* pCh = (LPSTR)(LPCTSTR)pStr[i];
printf("%s\n", pCh);
}
delete []pStr;
}
system("pause");
return nRetCode;
}
CString * SplitString(CString str, char split, int& iSubStrs)
{
int iPos = 0; //分割符位置
int iNums = 0; //分割符的总数
CString strTemp = str;
CString strRight;
//先计算子字符串的数量
while (iPos != -1)
{
iPos = strTemp.Find(split);
if (iPos == -1)
{
break;
}
strRight = strTemp.Mid(iPos + 1, str.GetLength());
strTemp = strRight;
iNums++;
}
if (iNums == 0) //没有找到分割符
{
//子字符串数就是字符串本身
iSubStrs = 1;
return NULL;
}
//子字符串数组
iSubStrs = iNums + 1; //子串的数量 = 分割符数量 + 1
CString* pStrSplit;
pStrSplit = new CString[iSubStrs];
strTemp = str;
CString strLeft;
for (int i = 0; i < iNums; i++)
{
iPos = strTemp.Find(split);
//左子串
strLeft = strTemp.Left(iPos);
//右子串
strRight = strTemp.Mid(iPos + 1, strTemp.GetLength());
strTemp = strRight;
pStrSplit[i] = strLeft;
}
pStrSplit[iNums] = strTemp;
return pStrSplit;
}
//
#include "stdafx.h"
#include "Test3.h"
#include <tchar.h>
#define _UNICODE
#define UNICODE
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
//最后一个参数返回的是子字符串的数量
CString * SplitString(CString str, char split, int& iSubStrs);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
//cout << (LPCTSTR)strHello << endl;
}
CString str = "sssdf sfds jieuri lj122 98098";
// CString str = "sfsfsf";
CString* pStr;
int iSubStrs;
pStr = SplitString(str, ' ', iSubStrs);
//如果子字符串的数量为1
if (iSubStrs == 1)
{
//Convert CString to char
char* pCh = (LPSTR)(LPCTSTR)str;
printf("%s\n", pCh);
}
else
{
//输出所有子字符串
for (int i = 0; i < iSubStrs; i++)
{
//Convert CString to char
char* pCh = (LPSTR)(LPCTSTR)pStr[i];
printf("%s\n", pCh);
}
delete []pStr;
}
system("pause");
return nRetCode;
}
CString * SplitString(CString str, char split, int& iSubStrs)
{
int iPos = 0; //分割符位置
int iNums = 0; //分割符的总数
CString strTemp = str;
CString strRight;
//先计算子字符串的数量
while (iPos != -1)
{
iPos = strTemp.Find(split);
if (iPos == -1)
{
break;
}
strRight = strTemp.Mid(iPos + 1, str.GetLength());
strTemp = strRight;
iNums++;
}
if (iNums == 0) //没有找到分割符
{
//子字符串数就是字符串本身
iSubStrs = 1;
return NULL;
}
//子字符串数组
iSubStrs = iNums + 1; //子串的数量 = 分割符数量 + 1
CString* pStrSplit;
pStrSplit = new CString[iSubStrs];
strTemp = str;
CString strLeft;
for (int i = 0; i < iNums; i++)
{
iPos = strTemp.Find(split);
//左子串
strLeft = strTemp.Left(iPos);
//右子串
strRight = strTemp.Mid(iPos + 1, strTemp.GetLength());
strTemp = strRight;
pStrSplit[i] = strLeft;
}
pStrSplit[iNums] = strTemp;
return pStrSplit;
}
- 分割字符串(CString)
- 分割字符串(CString版本)
- strtok CString 分割字符串
- cstring 分割字符串
- CString字符串分割
- CString分割字符串
- CString 分割字符串
- CString字符串分割
- CString分割字符串实现
- splitString CString 类型的字符串分割
- CString 分割
- cstring字符串分割成按空格子字符串
- CString分割字符串和截取字符串的成员函数
- VC++中按特定标志字符实现字符串到字符串数组的分割(CString -> Cstring[])
- VC++中按特定标志字符实现字符串到字符串数组的分割(CString -> Cstring[])
- 以定义的标志分割CString型字符串
- C++中以固定分隔符分割CString字符串
- MFC中按指定分隔符分割CString字符串
- 寻找第K大的数的方法总结
- 由 tpp.c:63: __pthread_tpp_change_priority failed 引发的思考
- gcc中的除法
- 各种ASP.net 技术详解(一)
- NSIS——“正在安装”步骤中,如何轮换显示图片
- 分割字符串(CString)
- android自动化测试技术
- 感受一下struts2框架设计及扩展考虑
- Android选择TextView的文字
- 【UML】UML几种图的绘制
- jqgrid导出CSV
- C++ string类
- 如何控制在一个软件中特殊的字符比如#都显示为红色呢?该字符是作为标签中的内容出现的,可能出现在JLABEL,JCheckBox,JCombox的标签中的,
- java 基本数据类型