有一个CString 变量 str。要获取其中所有的数字而且如果不是连续的数字要把这些数字赋给不同的int 型变量。
假如 str = "128abcd256abcde512"
那么获取的数字分别为 128,256,512,并能分别把他们赋给 i = 125;j = 256;k = 512;
有没有简单一点的方法,直接调用什么函数就能搞定的。而不是编写类似如下的代码.
CString str;
cin>>str;
int len = str.GetLength();
bool IsNum = false;
int number;
int count = 0;
int arr[100] = {0};
char c;
for(int i = 0;i <= len;)
{
c = str.GetAt(i);
if( c >= '0' && c <= '9')
{
IsNum = true;
number = c - '0';
int j = 0;
for( j = i + 1;j <= len && start;j++)
{
c = str.GetAt(j);
if(c >='0' && c <= '9')
{
number = number*10 + (c - '0');
}
else
{
IsNum = false;
}
}
arr[count] = number;
count++;
i = j;
}
else
{
i++;
}
}
假如 str = "128abcd256abcde512"
那么获取的数字分别为 128,256,512,并能分别把他们赋给 i = 125;j = 256;k = 512;
有没有简单一点的方法,直接调用什么函数就能搞定的。而不是编写类似如下的代码.
CString str;
cin>>str;
int len = str.GetLength();
bool IsNum = false;
int number;
int count = 0;
int arr[100] = {0};
char c;
for(int i = 0;i <= len;)
{
c = str.GetAt(i);
if( c >= '0' && c <= '9')
{
IsNum = true;
number = c - '0';
int j = 0;
for( j = i + 1;j <= len && start;j++)
{
c = str.GetAt(j);
if(c >='0' && c <= '9')
{
number = number*10 + (c - '0');
}
else
{
IsNum = false;
}
}
arr[count] = number;
count++;
i = j;
}
else
{
i++;
}
}
int arr[100] = {0};
CString str = "sd128abcd256abcde512wer";
while (!str.IsEmpty())
{
int offset = 1;
if (str.GetAt(0) >= '0' && str.GetAt(0) <= '9')
{
int n = atoi(str);
CString tmp; tmp.Format("%d", n);
offset = tmp.GetLength(); arr[count++] = n;
} str = str.Right(str.GetLength()-offset);
}