字符串分割

来源:百度文库 编辑:神马文学网 时间:2024/04/29 09:10:48

strsep

  原型:char *strsep(char **stringp, const char *delim);  功能:分解字符串为一组字符串。  示例:      #include   #include   int main(void)  {  char str[] = "root:x::0:root:/root:/bin/bash:";  char *buf;  char *token;  buf = str;  while((token = strsep(&buf, ":")) != NULL){  printf("%s\n", token);  }    return 0;  }    

编辑本段原型

  char *strtok(char *s, char *delim);

编辑本段功能

  分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。

编辑本段说明

  首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。  strtok在s中查找包含在delim中的字符并用NULL('')来替换,直到找遍整个字符串。  char * p = strtok(s,";");  p = strtok(null,";");  在调用的过程中,字串s被改变了,这点是要注意的。

编辑本段返回值

  从s开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。  所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。

编辑本段strtok函数在C和C++语言中的使用

  strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果  要保持原字符串的完整,可以使用strchr和sscanf的组合等。

c

  #include   #include   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;  }

c++

  #include   #include   using namespace std;  int main()  {  char sentence[]="This is a sentence with 7 tokens";  cout<<"The string to be tokenized is:\n"< 字符串分割 如何将字符串按指定长度分割(多字节安全)? 如何将字符串按指定长度分割(多字节安全)? 开发JAVA编程中字符串分割的两种方法 C#中使用string.Split方法来分割字符串的注意事项: C#中使用string.Split方法来分割字符串的注意事项: 字符串操作 字符串操作 字符串处理 分割图案 分割线条 动态分割 字符串hash算法比较|字符串,hash,算法 用 javascript 操作字符串 字符串函数大全 KMP字符串模式匹配 用 JavaScript 操作字符串 python字符串操作 Delphi字符串函数大全 VC中字符串换行 VB.NET字符串函数 Delphi字符串函数大全 Delphi字符串函数 Sybase字符串函数