Chinaunix

标题: 求一个拆字符串的写法(要效率高一些) [打印本页]

作者: 爱河之星    时间: 2006-04-27 15:15
标题: 求一个拆字符串的写法(要效率高一些)
例如:0|2304|1|1|031186342880|82147386342881|86342881|2001|127|03111234561|20060315190842|20060315190846|1|300||67|2006030400|1|3|
这里一行字符串,他们是用"|"分割,
现求一个算法以这个"|"做分隔符,最后结果写到一个数组里即可,各位大哥帮帮忙吧
作者: mike_chen    时间: 2006-04-27 15:35
这两天全是这个问题,你们是不是都是老师给统一安排的作业呀!
strstr
or
strtok
作者: 爱河之星    时间: 2006-04-27 15:42
能不能给一些例子
作者: nnnqpnnn    时间: 2006-04-27 15:46
连一个简单的函数都要例子的话,楼主怎么培养自学能力?
作者: 爱河之星    时间: 2006-04-27 15:48
俺现在是一个初学者,请多包涵
作者: tyc611    时间: 2006-04-27 15:48
要例子,可以到本版搜一下,多得不能再多了
作者: 爱河之星    时间: 2006-04-27 15:51
我都找了好久了,都没有找到
作者: nnnqpnnn    时间: 2006-04-27 16:00
SYNOPSIS
       #include <string.h>

       char *strstr(const char *haystack, const char *needle);

DESCRIPTION
       The  strstr() function finds the first occurrence of the substring nee-
       dle in the string haystack.  
RETURN VALUE
       The  strstr()  function  returns a pointer to the beginning of the sub-
       string, or NULL if the substring is not found.


SYNOPSIS
       #include <string.h>
       char *strtok(char *s, const char *delim);
DESCRIPTION
       The strtok() function can be used to parse the string  s  into  tokens.
       The  first call to strtok() should have s as its first argument. Subse-
       quent calls should have the first  argument  set  to  NULL.  Each  call
       returns  a  pointer  to the next token, or NULL when no more tokens are
       found.

       If a token ends with a delimiter, this delimiting  character  is  over-
       written  with a \0 and a pointer to the next character is saved for the
       next call to strtok().  The delimiter string delim may be different for
       each call.

       The  strtok_r()  function  is a reentrant version of the strtok() func-
       tion, which instead of using its own static buffer, requires a  pointer
       to  a user allocated char*. This pointer, the ptrptr parameter, must be
       the same while parsing the same string.

或者楼主到baidu google  随便一搜
作者: soul_of_moon    时间: 2006-04-27 16:20
原帖由 mike_chen 于 2006-4-27 15:35 发表
这两天全是这个问题,你们是不是都是老师给统一安排的作业呀!
strstr
or
strtok

应该是strchr吧
作者: stiandao    时间: 2006-04-27 16:24
int split(char sp, char *line, char **lst, int lst_len)
{
    int i;
    lst[0] = line;
    for (i=1; *line; line++)
    {
            if (*line == sp)
            {
                *line = '\0';
                if (i >= lst_len) break;
                    lst[i++] = line + 1;
            }
    }
    return i;
}

char *pList[80];
split('|',src,pList,80);
作者: 流川    时间: 2006-04-27 23:45
is sample

write a program and no libary
作者: atg    时间: 2006-04-28 12:56
自己写吧很容易的
作者: gamegod    时间: 2006-04-29 12:37
用boost中的tokenizer可以解决这个问题
  template <
        class TokenizerFunc = char_delimiters_separator<char>,
        class Iterator = std::string::const_iterator,
        class Type = std::string
  >
  class tokenizer
作者: 2eye    时间: 2006-04-29 12:51
string str;
vector<string> v;
while( cin.get(ch) ){
   if( ch != '|' ) str.push_back(ch);
   else{
       v.push_back(str);
       str = "";
   }
}
作者: apexg    时间: 2006-05-02 12:01
标题: 可以解决字段内容中存在"|"的情况么
一种情况是:12|asb|张弢fds|33543.33|
弢字的后半部分是竖线
另外一种情况是:12|asb|张?fds|33543.33|,即字段内容中有半个汉字或者乱码的情况.
各位高手,如何准确的拆分?使用标准c




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2