我想知道如何取一个字符串,用分隔符(例如空格)将其拆分为2,并将2个部分分配给2个单独的字符串。我尝试过使用strtok()
,但无济于事。
答案 0 :(得分:56)
#include <string.h>
char *token;
char line[] = "SEVERAL WORDS";
char *search = " ";
// Token will point to "SEVERAL".
token = strtok(line, search);
// Token will point to "WORDS".
token = strtok(NULL, search);
请注意,在某些操作系统上,strtok
手册页提及:
此界面已被strsep(3)废弃。
strsep
的示例如下所示:
char* token;
char* string;
char* tofree;
string = strdup("abc,def,ghi");
if (string != NULL) {
tofree = string;
while ((token = strsep(&string, ",")) != NULL)
{
printf("%s\n", token);
}
free(tofree);
}
答案 1 :(得分:11)
出于这样的目的,我倾向于使用strtok_r()而不是strtok()。
例如......
int main (void) {
char str[128];
char *ptr;
strcpy (str, "123456 789asdf");
strtok_r (str, " ", &ptr);
printf ("'%s' '%s'\n", str, ptr);
return 0;
}
这将输出......
'123456''789asdf'
如果需要更多分隔符,则循环。
希望这有帮助。
答案 2 :(得分:3)
char *line = strdup("user name"); // don't do char *line = "user name"; see Note
char *first_part = strtok(line, " "); //first_part points to "user"
char *sec_part = strtok(NULL, " "); //sec_part points to "name"
注意:strtok
修改字符串,所以不要将其指向字符串文字。
答案 3 :(得分:3)
你可以使用strtok() 示例:它适用于我
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
答案 4 :(得分:1)
如果您愿意更改原始字符串,只需将分隔符替换为\0
即可。原始指针将指向第一个字符串,并且分隔符指向第二个字符串后指向字符的指针。好处是您可以同时使用两个指针而无需分配任何新的字符串缓冲区。
答案 5 :(得分:1)
如果你分配了一个char数组,你可以在任何你想要的地方放一个'\0'
。
然后将新的char *指针指向新插入的'\0'
之后的位置。
这会破坏您的原始字符串,具体取决于您放置'\0'
答案 6 :(得分:1)
你可以这样做:
char str[] ="Stackoverflow Serverfault";
char piece1[20] = ""
,piece2[20] = "";
char * p;
p = strtok (str," "); // call the strtok with str as 1st arg for the 1st time.
if (p != NULL) // check if we got a token.
{
strcpy(piece1,p); // save the token.
p = strtok (NULL, " "); // subsequent call should have NULL as 1st arg.
if (p != NULL) // check if we got a token.
strcpy(piece2,p); // save the token.
}
printf("%s :: %s\n",piece1,piece2); // prints Stackoverflow :: Serverfault
如果您期望多个令牌,最好在while循环中调用对strtok
的第二次和后续调用,直到strtok
的返回值变为NULL
。
答案 7 :(得分:1)
这是您实现类似strtok()
功能的方法(取自a BSD licensed string processing library for C, called zString)。
以下函数与标准strtok()
的区别在于它识别连续分隔符的方式,而标准strtok()
则不然。
char *zstring_strtok(char *str, const char *delim) {
static char *static_str=0; /* var to store last address */
int index=0, strlength=0; /* integers for indexes */
int found = 0; /* check if delim is found */
/* delimiter cannot be NULL
* if no more char left, return NULL as well
*/
if (delim==0 || (str == 0 && static_str == 0))
return 0;
if (str == 0)
str = static_str;
/* get length of string */
while(str[strlength])
strlength++;
/* find the first occurance of delim */
for (index=0;index<strlength;index++)
if (str[index]==delim[0]) {
found=1;
break;
}
/* if delim is not contained in str, return str */
if (!found) {
static_str = 0;
return str;
}
/* check for consecutive delimiters
*if first char is delim, return delim
*/
if (str[0]==delim[0]) {
static_str = (str + 1);
return (char *)delim;
}
/* terminate the string
* this assignmetn requires char[], so str has to
* be char[] rather than *char
*/
str[index] = '\0';
/* save the rest of the string */
if ((str + index + 1)!=0)
static_str = (str + index + 1);
else
static_str = 0;
return str;
}
以下是演示用法的示例代码
Example Usage
char str[] = "A,B,,,C";
printf("1 %s\n",zstring_strtok(s,","));
printf("2 %s\n",zstring_strtok(NULL,","));
printf("3 %s\n",zstring_strtok(NULL,","));
printf("4 %s\n",zstring_strtok(NULL,","));
printf("5 %s\n",zstring_strtok(NULL,","));
printf("6 %s\n",zstring_strtok(NULL,","));
Example Output
1 A
2 B
3 ,
4 ,
5 C
6 (null)
你甚至可以使用while循环(标准库&#39; s strtok()
会在这里给出相同的结果)
char s[]="some text here;
do {
printf("%s\n",zstring_strtok(s," "));
} while(zstring_strtok(NULL," "));