本文目录一览:
1、c++里面,函数strtok怎么用?
2、关于c语言字符串中切割函数strtok的用法
3、我想用c语言中的strtok函数得到一个字符串中由分隔符分割的某些关键字,并处理
4、请问,C语言中,对带分隔符的字符串如何分割?
5、C语言中strtok用法
c++里面,函数strtok怎么用?
strtok
:
分解字符串为一组字符串。s
为要分解的字符串,delim
为分隔符字符串。首次调用时,s
指向要分解的字符串,之后再次调用要把 s
设成NULL
。
功能:
分解字符串为一组字符串。s
为要分解的字符串,delim
为分隔符字符串。
例如:strtok("abc,def,ghi", ",")
,最后可以分割成为 abc def ghi
。尤其在点分十进制的IP中提取应用较多。
函数使用:
strtok
函数会破坏被分解字符串的完整,调用前和调用后的s
已经不一样了。如果要保持原字符串的完整,可以使用strchr
和sscanf
的组合等。
C语言示例:
#include <string.h>
#include <stdio.h>
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 <iostream>
#include <cstring>
using namespace std;
int main()
{
char sentence[] = "This is a sentence with 7 tokens";
cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n";
char *tokenPtr = strtok(sentence, " ");
while (tokenPtr != NULL) {
cout << tokenPtr << '\n';
tokenPtr = strtok(NULL, " ");
}
return 0;
}
函数第一次调用需设置两个参数。第一次分割的结果,返回串中第一个 ,
之前的字符串,也就是上面的程序第一次输出 abc
。
第二次调用该函数 strtok(NULL, ",")
,第一个参数设置为 NULL
。结果返回分割依据后面的字串,即第二次输出 d
。
strtok
是一个线程不安全的函数,因为它使用了静态分配的空间来存储被分割的字符串位置。
线程安全的函数叫 strtok_r
。
运用 strtok
来判断 IP 或者 MAC 的时候务必要先用其他的方法判断 .
或 :
的个数,因为用 strtok
截断的话,比如:"192..168.0...8..."
这个字符串,strtok
只会截取四次,中间的 ...
无论多少都会被当作一个 key。
其他相关信息:
下面的说明摘自于最新的 Linux 内核 2.6.29,说明了这个函数已经不再使用,由速度更快的 strsep()
代替。
/**
* linux/lib/string.c
* Copyright (C) 1991, 1992 Linus Torvalds
*
* Stupid library routines.. The optimized versions should generally be found
* as inline code in asm-xx/string.h
* These are buggy as well..
*
* Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
* - Added strsep() which will replace strtok() soon (because strsep() is
* reentrant and should be faster). Use only strsep() in new code, please.
*
* Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
* Matthew Hawkins <matt@mh.dropbear.id.au>
* - Kissed strtok() goodbye
*/
关于c语言字符串中切割函数strtok的用法
strtok()
函数并不像你想的那样可以一次切割字串。需要多次循环,第二次时需要用 p = strtok(NULL, " ")
这样的形式。
示例代码:
void main()
{
char test1[] = "Hello C World";
char *p;
p = strtok(test1, " ");
while (p) {
printf("%s\n", p);
p = strtok(NULL, " ");
}
return 0;
}
运行结果:
Hello
C
World
我想用c语言中的strtok函数得到一个字符串中由分隔符分割的某些关键字,并处理
示例代码:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[100], spl[10], *p;
fputs("请输入字符串 : ", stdout);
gets(str);
fputs("请输入分割符 : ", stdout);
gets(spl);
p = strtok(str, spl);
while (p != NULL) {
puts(p);
p = strtok(NULL, spl);
}
return 0;
}
请问,C语言中,对带分隔符的字符串如何分割?
C/C++中的 Split
函数是 strtok()
,其函数原型如下:
char *strtok(char *str, const char *delimiters);
函数说明:
strtok()
用来将字符串分割成一个个片段。参数 str
指向欲分割的字符串,参数 delimiters
则为分割字符串,当 strtok()
在参数 str
的字符串中发现到参数 delimiters
的分割字符时则会将该字符改为 \0
字符。在第一次调用时,strtok()
必需给予参数 str
字符串,往后的调用则将参数 str
设置成 NULL
。每次调用成功则返回下一个分割后的字符串指针。
返回值:
返回下一个分割后的字符串指针,如果已无从分割则返回 NULL
。
示例代码:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "a,b,c,d*e";
const char *split = ",";
char *p;
p = strtok(str, split);
while (p != NULL) {
printf("%s\n", p);
p = strtok(NULL, split);
}
getchar();
return 0;
}
输出结果:
a
b
c
d*e
因为 delimiters
支持多个分割符,将上面示例中的语句行:
const char *split = ",";
改成:
const char *split = ",*"; // 用逗号(,)和星号(*)对字符串进行分割
这样输出结果将如下所示:
a
b
c
d
e
C语言中strtok用法
一般来说,条件关键词(if
、else
、else if
、for
、while
)只能作用于紧随其后的第一句代码。
{}
的作用,你可以这么理解:是把被括起来的所有代码当成一句代码送给关键词来处理。
注意:被括起来的可以是多句,当然也可以是一句。
示例:
if (a == b)
printf("a == b");
printf("a != b");
这个时候第二个 printf
对 if
来说不是紧随的第一句,所以不受 if
限制,一定会输出。
if (a == b) {
printf("a == b");
printf("a != b");
}
这个时候,整个大括号里的(两句 printf
)就是紧随 if
的第一句代码了。