运用函数AfxExtractSubString可以解决.
BOOL AfxExtractSubString(CString& rString, LPCTSTR lpszFullString,int iSubString, TCHAR chSep = '\n') 参数说明: rString 得到的字符串;lpszFullString 待分割的字符串;iSubString 要得到第几个字符串;chSep 个子串之间的分隔符,默认是回车;
举例:
//实现的功能是把m_day1中的数字存放到字符串数组str中.
CString m_day1=”2009/11/07-12:12:12”;
CString tempstr1,tempstr2,tempstr3;
CString str[6];
for(int i=0;i<3;i++)
{
AfxExtractSubString(tempstr1,m_day1,i,'/');
str[i]=tempstr1;
}//tempstr1=="07-12:12:12"
for(i=0;i<2;i++)
{
AfxExtractSubString(tempstr2,tempstr1,i,'-');
str[i+2]=tempstr2;
}//tempstr2=="12:12:12"
for(i=0;i<3;i++)
{
AfxExtractSubString(tempstr3,tempstr2,i,':');
str[i+3]=tempstr3;
}
//如果要把字符串数组变为整型数组只要使用int atoi(const char*)函数即可.
函数AfxExtractSubString但是有两个限制: 1.仅仅能在MFC下使用的函数 2.分隔只能使用字符,不能使用字符串。
BOOL AfxExtractSubString(CString& rString, LPCTSTR lpszFullString,int iSubString, TCHAR chSep = '\n') 参数说明: rString 得到的字符串;lpszFullString 待分割的字符串;iSubString 要得到第几个字符串;chSep 个子串之间的分隔符,默认是回车;
举例:
//实现的功能是把m_day1中的数字存放到字符串数组str中.
CString m_day1=”2009/11/07-12:12:12”;
CString tempstr1,tempstr2,tempstr3;
CString str[6];
for(int i=0;i<3;i++)
{
AfxExtractSubString(tempstr1,m_day1,i,'/');
str[i]=tempstr1;
}//tempstr1=="07-12:12:12"
for(i=0;i<2;i++)
{
AfxExtractSubString(tempstr2,tempstr1,i,'-');
str[i+2]=tempstr2;
}//tempstr2=="12:12:12"
for(i=0;i<3;i++)
{
AfxExtractSubString(tempstr3,tempstr2,i,':');
str[i+3]=tempstr3;
}
//如果要把字符串数组变为整型数组只要使用int atoi(const char*)函数即可.
函数AfxExtractSubString但是有两个限制: 1.仅仅能在MFC下使用的函数 2.分隔只能使用字符,不能使用字符串。
解决方案 »
- vs2008添加自定义消息出了问题
- 如何用VC同时产生ACCESS97和ACCESS2000两种格式的数据库?
- 一个简单的ActiveX的Stock问题:
- 请问:下面的两句是何意思???#progma 是干什么的?
- 请高手帮忙看看,此多媒体播放文件哪有问题?
- vc初学者得困惑,请各位长老帮忙。
- 在mdi工程(基类是CFormView)中插入IDD_FORMVIEW对话框,为什么用菜单命令调用DoModal()显示时系统没反应??
- 请教:用哪些api函数可以设置window系统的环境变量?
- 用VC++编写的simple dll编译后为何VB不能调用??
- 关于整蛊播放器, 程序员的耻辱...
- 总的专家分为 666 高兴啊!虎年大顺 特来散分……请管理员先不要移到非技术区,我会在结贴时自己移过去!……
- QQ远程协助全屏后顶端的工具条如何实现???
atof(将字符串转换成浮点型数)
相关函数 atoi,atol,strtod,strtol,strtoulsuch as: CString strTemp="100";
int i=0;
i=atoi(strTemp);
if(i == 100) printf("ok:i=100");
run result:
ok:i=100 我也是正在学习的哦
test="1234";
char buff[10];
atoi(test,buff,10);
pritf("%d",buff);
Example/* ATOF.C: This program shows how numbers stored
* as strings can be converted to numeric values
* using the atof, atoi, and atol functions.
*/#include <stdlib.h>
#include <stdio.h>void main( void )
{
char *s; double x; int i; long l; s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i ); s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
Outputatof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854
sscanf();
逆向用sprintf();