怎样提取字符串中的数字啊?

2026年09月09日 06:20
有5个网友回答
网友(1):

1、如下图,要提取#后面的字符,也即红色的“SDK”到B列。

2、首先,在B2中输入公式:

=FIND("#",A2)

返回#在字符串中的位置,#在A2单元格文本中是第6个字符。

3、知识点说明:

FIND()函数查找第一参数在第二参数中的位置。如下图,查找“B”在“ABCD”中是第几个字符。第一参数是要查找的字符“B”,第二参数是被查找的字符串。最终返回“B”在“ABCD”中是第2个字符。

4、然后,在B2中输入公式:

=MID(A2,FIND("#",A2)+1,99)

这样,就提取出了#后的字符。

5、知识点说明:

MID()函数返回从字符串中制定字符开始若干个字符的字符串。如下图,MID()函数返回“ABCDE”字符串中从第2个字符开始的连续3个字符,也就是返回“BCD”。

6、综上,=MID(A2,FIND("#",A2)+1,99)的意思就是从A2单元格#字符后面的一个字符起,取长度为99的字符串。其中的99是一个较大的数字,能涵盖#后字符的最大长度即可。

网友(2):

#include
#include
using namespace std;

void main()
{
char *ps="Point('Point4:LOCATION',(45.00,50.00,0.00))";
string str(ps);

// 找第一个'('
basic_string ::size_type index = string.npos;
index = str.find_first_of('(');
if (index != string.npos)
str = str.substr(index + 1);

// 找第二个'('
index = string.npos;
index = str.find_first_of('(');
if (index != string.npos)
str = str.substr(index + 1);

// 找第一个')'
index = string.npos;
index = str.find_first_of(')');
if (index != string.npos)
str = str.substr(0, index);

// 分析两个整数
string a, b;
int p1, p2;
index = string.npos;
index = str.find_first_of(',');
a = str.substr(0, index);
str = str.substr(index + 1);

index = string.npos;
index = str.find_first_of(',');
b = str.substr(0, index + 1);

p1 = atoi(a.c_str());
p2 = atoi(b.c_str());

cout << "P1 = " << p1 << endl;
cout << "P2 = " << p2 << endl;
getchar();
}

///////////////////////////////////////
-_-|| 所以你小兄弟不是笨唉,是懒!!

网友(3):

这写法很愚蠢....
请笑纳..
有待改进,供你参考
#include
using std::cout;
using std::endl;

int main()
{
char *ps="Point('Point4:LOCATION',(45.00,50.00,0.00))";
int a,b,t;
bool flag;
flag = 0;
for(int i = 0; i< strlen(ps);i++)
{
if(ps[i]>='0'&&ps[i]<='9'&&ps[i+1]>='0'&&ps[i+1]<='9'&&ps[i+2]=='.'){
if(flag == 0){
a = ps[i]-'0';
a *= 10;
t = ps[i+1] - '0';
a +=t;
}
flag = 1;
}
if(ps[i]>='0'&&ps[i]<='9'&&ps[i+1]>='0'&&ps[i+1]<='9'&&ps[i+2]=='.'){
b = ps[i]-'0';
b *= 10;
t = ps[i+1] - '0';
b +=t;
}
}
cout << a << endl << b << endl;
return 0;
}

网友(4):

嗯,把这些数字放到数组中了。

不过把Point4的4也提取出来了,值为0.0的没有管,这合你要求不?

#include

void main()
{
char *ps="Point('Point4:LOCATION',(45.00,50.00,0.00))";
float a[10]={0};
int n=0,i;
char *p;

p=ps;
while(*p!='\0')
{
if(*p<='9'&&*p>='0')
{//如果是字符
a[n]=a[n]*10+*p-'0';
}
else if(a[n]>0)
{
n++;
}
p++;
}
for(i=0;i{
printf("%f ",a[i]);
}
}

补充:
我只是给你一个参考,毕竟这个毕业设计是你来作,而不是我。
只要对你有帮助,我的目的就达到了
而不是说我要达到完美无缺

网友(5):

\*完整版,无论有多少数,什么样的浮点,大小通吃,向楼上的学习C++*\
#include
#include
#include
using namespace std;
void main()
{
char *ps="Point('Point4:LOCATION'(455.055,456.12,889.22)'Point3:LOCATION'(55.06,56.70),(145.09,150.00,0.00))";

vector s;
while(*ps!='\0')
{
while(*ps!=')') ps++;
ps--;
while(*ps!='(') ps--;

do
{ float a=0,b=0;
int p=1;
ps++;
while(*ps>='0'&&*ps<='9')
{a=a*10+(*ps-'0'); ps++;}
if(*ps=='.') ps++;
while(*ps>='0'&&*ps<='9')
{b=b+(float)(*ps-'0')/pow(10,p); p++;ps++;}
a+=b;
s.push_back(a);
}
while(*ps!=')');

ps++;
if(*ps++==')') break;
}
for (int i=0;i cout<}