CString字符串中数字的提取

假设CString类型的字符串“192.168.1.1”

void main()
{
 CString str = "192.168.1.1";
 int a,b,c,d;
 int pos;
 pos = str.Find('.');
 CString temp;
 temp = str.Left(pos);
 a = atoi(temp);
 str = str.Right(str.GetLength()-pos-1);
 pos = str.Find('.');
 temp = str.Left(pos);
 b = atoi(temp);
 str = str.Right(str.GetLength()-pos-1);
 pos = str.Find('.');
 temp = str.Left(pos);
 c = atoi(temp);
 str = str.Right(str.GetLength()-pos-1);
 d = atoi(str);
 printf("a=%d\nb=%d\nc=%d\nd=%d\n",a,b,c,d);
}

输出:

a=192
b=168
c=1
d=1


vc控制台下还需要进行如下设置才能用MFC中的CString
工程中设置:工程项目属性->  常规->  MFC的使用-> 在静态库中使用DLL

猜你喜欢

转载自blog.csdn.net/zomixi/article/details/79584019