请问如何将数字字符串换成数字以及在字符串中提取数字字符串。

shazhudedaxia 2005-07-11 07:33:09
如:字符串123,用什么办法可以转换成数值类型的123呢?

还有假如一个字符串"123,456,178,234",如何把这些字符取出来放在一个数组里呢(也就是去掉,号)?

请给出个程序代码示例一下如何啊。
先谢谢了。
...全文
117 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shazhudedaxia 2005-07-11
  • 打赏
  • 举报
回复
高手,高手,我结贴了啊,谢谢你们。
第一次发文章,30分钟后就帮我解决了困饶我半天的问题。
呵呵。
jsjjms 2005-07-11
  • 打赏
  • 举报
回复
函数名: strtok
功 能: 查找由在第二个串中指定的分界符分隔开的单词
用 法: char *strtok(char *str1, char *str2);
程序例:

#include <string.h>
#include <stdio.h>

int main(void)
{
char input[16] = "abc,d";
char *p;

/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%s\n", p);

/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%s\n", p);
return 0;
}
晨星 2005-07-11
  • 打赏
  • 举报
回复
(1)int i = atoi("123");
(2)使用strtok,详查MSDN。
foochow 2005-07-11
  • 打赏
  • 举报
回复
后面那个用strtok分割字符串就可以
foochow 2005-07-11
  • 打赏
  • 举报
回复
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
string temp="1235";
stringstream ps(temp);
int result;
ps>>result;
cout<<result<<endl;
return 0;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧