大家好,欢迎来到IT知识分享网。
在程序中,解析用户输入的参数(命令行参数)是很常见的操作,本文将讲解C语言中常见的一些解析字符串函数使用方法。
1 strchr
1.1 描述
1.2 声明
1.3 实例
#include <stdio.h> #include <string.h> int main() {
const char *str = "https://10.229.89.210/home/data/1.txt"; const char ch = 'd'; char *ptr; ptr = strchr(str, ch); if (ptr != NULL) {
printf("字符 '%c 出现的位置为 %ld。\n",ch, ptr - str + 1); printf("|%c| 之后的字符串是 - |%s|\n", ch, ptr); } else {
printf("没有找到字符 'd' 。\n"); } return (0); }
2 strrchr
2.1 描述
char *strrchr(const char *str, int c) 在参数 str 所指向的字符串中搜索最后一次出现字符 c(一个无符号字符)的位置。其原型定义在头文件 <string.h> 中。
2.2 声明
2.3 实例
#include <stdio.h> #include <string.h> int main() {
const char *str = "https://10.229.89.210/home/data/1.txt"; const char ch = '/'; char *ptr; ptr = strrchr(str, ch); if (ptr != NULL) {
printf("字符 '%c 出现的位置为 %ld。\n",ch, ptr - str + 1); printf("|%c| 之后的字符串是 - |%s|\n", ch, ptr); } else {
printf("没有找到字符 'd' 。\n"); } return (0); }
3 strtok
3.1 描述
char *strtok(char *str, const char *delim) 分解字符串 str 为一组字符串,delim 为分隔符。
3.2 声明
3.3 实例
3.3.1 实例1
#include <stdio.h> #include <string.h> #include<stdlib.h> int main() {
char *str = "https://10.229.89.210/home/data/1.txt"; char *str1 = malloc(sizeof(char) * (strlen(str) + 1)); strcpy(str1, str); const char ch = '/'; char *ptr; char *ptr1; ptr = strtok(str, ":"); //ptr1 = strtok(str1, ":"); if (ptr != NULL) {
// printf("%s\n", str); // strtok会修改 printf("%s\n", ptr); // printf("\n%s\n\n", ptr1); // printf("\n|%c| 之后的字符串是 - |%s|\n\n", ch, ptr); } else {
printf("\n没有找到字符 'd' 。\n"); } return (0); } 结果:段错误 (核心已转储)
3.3.2 实例2
#include <stdio.h> #include <string.h> #include<stdlib.h> int main() {
char *str = "https://10.229.89.210/home/data/1.txt"; char *str1 = malloc(sizeof(char) * (strlen(str) + 1)); strcpy(str1, str); // char str2[64]; strcpy(str2, str); ==> 数组也可以,如果不习惯 malloc/free匹对使用 const char ch = '/'; char *ptr; char *ptr1; //ptr = strtok(str, ":"); ptr1 = strtok(str1, ":"); if (ptr1 != NULL) {
// printf("%s\n", str); // strtok会修改str参数值 // printf("%s\n", ptr); printf("\n%s\n\n", ptr1); // printf("\n|%c| 之后的字符串是 - |%s|\n\n", ch, ptr); } else {
printf("\n没有找到字符 'd' 。\n"); } free(str1); return (0); } 结果正确: https
4 strtok_r
比如:按空格分割 字符串 “first second third”,
分第一次得字串”first”,然后saveptr指向了”second third”
#include <stdio.h> #include <string.h> #include<stdlib.h> int main() {
// Our input string char input_string[] = "https:/10.229.89.210/home/data/1.txt"; // Our output token list char **token_list = NULL; token_list = (char **)malloc(sizeof(char *) * 64); // A pointer, which we will be used as the context variable // Initially, we will set it to NULL char *context = NULL; // To get the value of the context variable, we can pass it's address // strtok_r() to automatically populate this context variable, and refer // it's context in the future char *token = strtok_r(input_string, "/", &context); int num_tokens = 0; // Index to token list. We will append to the list while (token != NULL) {
// Keep getting tokens until we receive NULL from strtok() token_list[num_tokens] = strdup(token); // Copy to token list num_tokens++; token = strtok_r(NULL, "/", &context); // We pass the context variable to strtok_r } // Print the list of tokens printf("Token List:\n"); for (int i = 0; i < num_tokens; i++) {
printf("%s\n", token_list[i]); } free(token_list); return 0; }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/130083.html