简体   繁体   English

在没有 strtok() 和 strsep() 的标准 C 中拆分字符串?

[英]Split a String in Standard C without strtok() and strsep()?

Let's say that we have the following:假设我们有以下内容:

char mystring[PMAX];
char strncpy( mystring, "hello;this;is;my;string;how;are kljasdf; you?" , PMAX);

Split a String in Standard C without strtok() and strsep()?在没有 strtok() 和 strsep() 的标准 C 中拆分字符串? Which reliable, portable or universal on any C standard, would you recommend to return the given part of the string?在任何 C 标准上哪个可靠、可移植或通用,您会建议返回字符串的给定部分吗?

Let' say:说吧:

strncpy( myoutput, strsplit( mystring, ';' , 6), PMAX );

The content of myoutput would be how (<- which is the sixth one, with ';' delimiter). myoutput 的内容将是 how (<- 这是第六个,带有 ';' 分隔符)。

The best would be to have just these and only to be readily portable on any machine(s):最好的办法是只拥有这些,并且可以在任何机器上轻松便携:

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

I am looking forward to hearing you.我期待着听到你的声音。

对于可预测的解析,没有什么比状态机更胜一筹了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM