救急!

nanxue_jiang 2003-01-28 08:03:34
如何分割字符串?
举例:
今有一字符串:str_test="|01+02+03+04+05|",请问如何把01,02,03,04,05开来,分别送给str_01,str_02,str_03,str_04,str_05?
请举例(最好是能提供源码)。


祝大家春节快乐!
...全文
49 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xdspower 2003-01-28
  • 打赏
  • 举报
回复
是strtok,battlet(battlet)的答案就可以了,我想楼主要问的也是这个意思。如果仅仅是分析"|01+02+03+04+05|",楼主还不如直接赋值了
xdspower 2003-01-28
  • 打赏
  • 举报
回复
用strake函数,专门用来分割字符串的,可能拼错了,不过这个函数十分好用的。
ESr 2003-01-28
  • 打赏
  • 举报
回复
老实点,逐个扫。
battlet 2003-01-28
  • 打赏
  • 举报
回复
转:(msdn)
/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/

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

char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}

Output
A string of ,,tokens
and some more tokens

Tokens:
A
string
of
tokens
and
some
more
tokens



孩皮妞野 2003-01-28
  • 打赏
  • 举报
回复
取决于你的串的固定程度,如果是固定的,可以更简单。
char * str_test="|01+02+03+04+05|";

str_01=str_test+1;
str_01[2]=0;
str_02=str_01+3;
str_02[2]=0;
依此类推。
如果不想破坏原串,可以先制作一个副本。



windcsn 2003-01-28
  • 打赏
  • 举报
回复
写如变量比较麻烦,你的先判断有多少个数,然后定义多少个变量,后来在按照哪个循环放入
windcsn 2003-01-28
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
string getStr(string src,int beg,int end);
void main ()
{
string a="|01+02+03+04|";
int plus_pos = 0;
plus_pos=a.find("|");
string b= a;
int i =0;
while(plus_pos!=-1)
{
string temp = getStr(b,plus_pos+1,plus_pos+2);
cout<<temp<<endl;
b= b.substr(plus_pos+3,b.size()-1);
plus_pos = b.find("+");
}
}
string getStr(string src,int beg,int end)
{
return src.substr(beg,end);
}
nanxue_jiang 2003-01-28
  • 打赏
  • 举报
回复
首先,谢谢大家! 我的字符串是可变的,但中间都是由+号隔开的。我的编译环境是SCO UNIX 5.05。

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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