c - Strtok 仅输出字符串的一部分
问题描述
#include <stdio.h>
#include <string.h>
int main(){
char name[] = "eseumdesconhecidolheoferecerflores.issoeimpulse.cities";
char *str;
printf("%s\n", name)
str = strtok(name, ".cities");
printf("%s\n", str);
return 0;
}
这是输出:
eseumdesconhecidolheoferecerflores.issoeimpulse.cities
umd
我根本不知道发生了什么。我想要的是 strtok 的输出是一个指向"eseumdesconhecidolheoferecerflores.issoeimpulse"
解决方案
strtok的 delimiter 参数是一个字符串,其中包含用于分隔字符串的单个字符。
您指定了分隔符.
, c
, i
, t
, e
, 和s
.
因此,输出是umd
第一个标记也就不足为奇了,因为它被分隔符字符串中的字符包围。
如果要查找整个字符串,则应strstr
改为使用。
例如:
char name[] = "eseumdesconhecidolheoferecerflores.issoeimpulse.cities";
char *pos;
pos = strstr(name, ".cities");
if (pos)
{
*pos = '\0';
printf("%s\n", name);
}
推荐阅读
- callback - 如何存储和访问字符串 dcc.Input 以加载新模型并使用图形、下拉列表等更新现有用户界面...?
- laravel - 带有 JavaScript 的 laravel 路由辅助参数
- python - 无法让 functionapp create 在 Azure 上与 Python 一起使用
- flutter - 如何在我的 Flutter 应用程序的某些屏幕中保持底部导航栏的持久性
- python - 函数中的 For 循环 - 不遍历字符串
- android - 导航到android中的另一个片段后如何在片段中保存ListView滚动位置?
- sql - Postgresql 如何在 2 个日期之间计算取决于另一个表
- java - 没有 WSDL 的 Web 服务
- javascript - 我似乎无法让我的 Img 显示在我的网页上
- python-3.x - 如何将字典中的值提取到新列中?