从C中的字符串中获取子字符串
我有一串“圣诞快乐”。我想把这个分别为“快乐”和“圣诞节”。在Objective c中,我可以通过从C中的字符串中获取子字符串,c,whitespace,substring,C,Whitespace,Substring,我有一串“圣诞快乐”。我想把这个分别为“快乐”和“圣诞节”。在Objective c中,我可以通过组件(通过字符串分隔):轻松地完成这项任务。如何在C中执行此操作?您可以使用在C中拆分字符串。您可以使用在C中拆分字符串。尝试查找尝试查找纯C中的标准方法是使用,尽管这是一个相当危险且具有破坏性的函数(它修改传入的字符缓冲区) 在C++中,有更好的方法;请参见此处:纯C中的标准方法是使用,尽管这是一个相当危险且具有破坏性的函数(它修改传入的字符缓冲区) 在C++中,有更好的方法;请参见此处:您必须编
组件(通过字符串分隔):
轻松地完成这项任务。如何在C中执行此操作?您可以使用在C中拆分字符串。您可以使用在C中拆分字符串。尝试查找尝试查找纯C中的标准方法是使用,尽管这是一个相当危险且具有破坏性的函数(它修改传入的字符缓冲区)
在C++中,有更好的方法;请参见此处:纯C中的标准方法是使用,尽管这是一个相当危险且具有破坏性的函数(它修改传入的字符缓冲区)
在C++中,有更好的方法;请参见此处:您必须编写自己的函数。
C库包括strtok()、strspn()和strcspn(),您称之为字符串的是字符数组(以
\0
结尾)。您必须编写自己的函数。
C库包括strtok()、strspn()和strcspn(),您称之为字符串的是字符数组(以
\0
结尾)。用于子字符串。用于标记化/拆分使用。用于子字符串使用。用于标记化/拆分使用。strtok是字符串标记化的通用解决方案。一种更简单、更有限的方法是使用strhr:
#include <string.h> // strchr, strcpy
#include <stddef.h> // NULL
const char str[] = "Merry Christmas";
const char* ptr_merry = str;
const char* ptr_christmas;
ptr_christmas = strchr(str, ' ');
if(ptr_christmas != NULL)
{
ptr_christmas++; // point to the first character after the space
}
// optionally, make hard copies of the strings, if you want to alter them:
char hardcpy_merry[N];
char hardcpy_christmas[n];
strcpy(hardcpy_merry, ptr_merry);
strcpy(hardcpy_christmas, ptr_christmas);
#包括//strchr、strcpy
#include//NULL
const char str[]=“圣诞快乐”;
const char*ptr_merry=str;
康斯特查尔*ptr_圣诞节;
ptr_christmas=strchr(str.);
如果(ptr_圣诞!=NULL)
{
ptr_christmas++;//指向空格后的第一个字符
}
//(可选)如果要更改字符串,请制作其硬拷贝:
查尔·哈德普·梅里[N];
圣诞老人;
strcpy(hardcpy_mery,ptr_mery);
strcpy(hardcpy_christmas,ptr_christmas);
strtok是字符串标记化的通用解决方案。一种更简单、更有限的方法是使用strhr:
#include <string.h> // strchr, strcpy
#include <stddef.h> // NULL
const char str[] = "Merry Christmas";
const char* ptr_merry = str;
const char* ptr_christmas;
ptr_christmas = strchr(str, ' ');
if(ptr_christmas != NULL)
{
ptr_christmas++; // point to the first character after the space
}
// optionally, make hard copies of the strings, if you want to alter them:
char hardcpy_merry[N];
char hardcpy_christmas[n];
strcpy(hardcpy_merry, ptr_merry);
strcpy(hardcpy_christmas, ptr_christmas);
#包括//strchr、strcpy
#include//NULL
const char str[]=“圣诞快乐”;
const char*ptr_merry=str;
康斯特查尔*ptr_圣诞节;
ptr_christmas=strchr(str.);
如果(ptr_圣诞!=NULL)
{
ptr_christmas++;//指向空格后的第一个字符
}
//(可选)如果要更改字符串,请制作其硬拷贝:
查尔·哈德普·梅里[N];
圣诞老人;
strcpy(hardcpy_mery,ptr_mery);
strcpy(hardcpy_christmas,ptr_christmas);