如何使用两个正斜杠提取字符串
how to extract a string using two forward slashes
我有一个字符串,它总是有两个正斜杠,如 /709/nviTemp1
我想从该字符串中提取 /709/
并在 char* 中提取 return,我将如何使用 strstr 来达到这个目的?
我也可能在路径中有很多正斜杠,例如 /709/nvitemp1/d/s/
所以我只需要拿到第一个令牌/709/
为此尝试使用 strtok
。 strtok
根据分隔符将字符串拆分为不同的标记。像这样:
char str[100] = "/709/nviTemp1";
char delimiter[2] = "/";
char *result;
char *finalresult;
result = strtok(str, delimiter); // splits by first occurence of '/', e.g "709"
strcat(finalresult,"/");
strcat(finalresult, result);
strcat(finalresult,"/");
printf("%s",finalresult);
请注意 strtok 会修改您传递给它的原始字符串。
要执行您所询问的任务,以下代码就足够了。如果您需要更通用的解决方案,答案显然会有所不同。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str = "/709/nviTemp1";
char *delims = "/";
char *strCopy;
char *tmpResult;
strCopy = strdup(str);
tmpResult = strtok(strCopy, delims);
// +1 for the first slash, +1 for the second slash, + another for the terminating NULL
char *finalResult = (char*)calloc(strlen(tmpResult) + 3, 1);
strcat(finalResult, "/");
strcat(finalResult, tmpResult);
strcat(finalResult, "/");
free(strCopy);
printf("%s",finalResult);
}
输出:
/709/
使用 strchr 查找第一个斜杠。前进指针并找到第二个斜线。将指针前进并设置为 '[=11=]'
.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main (int argc , char *argv[]) {
char *tok;
char text[] = "/709/nvitemp1/d/s/";
if ( ( tok = strchr ( text, '/')) != NULL) {//find first /
tok++;
if ( ( tok = strchr ( tok, '/')) != NULL) {//find second /
tok++;
*tok = '[=10=]';
printf ( "%s\n", text);
}
}
return 0;
}
您可以这样尝试:
char str[100] = "/709/nviTemp1";
char resu[100];
char *tmp;
tmp = strchr(str+1, '/');
strncpy(resu, str, (size_t)(tmp - str) + 1);
resu[(size_t)(tmp - str) + 1] = '[=10=]';
strchr
搜索第一个 '/',但从 str+1
开始会跳过真正的第一个。然后在开始和找到'/'之间计算"size"并使用strncpy
复制内容,并添加尾随'\0'。
我有一个字符串,它总是有两个正斜杠,如 /709/nviTemp1
我想从该字符串中提取 /709/
并在 char* 中提取 return,我将如何使用 strstr 来达到这个目的?
我也可能在路径中有很多正斜杠,例如 /709/nvitemp1/d/s/
所以我只需要拿到第一个令牌/709/
为此尝试使用 strtok
。 strtok
根据分隔符将字符串拆分为不同的标记。像这样:
char str[100] = "/709/nviTemp1";
char delimiter[2] = "/";
char *result;
char *finalresult;
result = strtok(str, delimiter); // splits by first occurence of '/', e.g "709"
strcat(finalresult,"/");
strcat(finalresult, result);
strcat(finalresult,"/");
printf("%s",finalresult);
请注意 strtok 会修改您传递给它的原始字符串。
要执行您所询问的任务,以下代码就足够了。如果您需要更通用的解决方案,答案显然会有所不同。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str = "/709/nviTemp1";
char *delims = "/";
char *strCopy;
char *tmpResult;
strCopy = strdup(str);
tmpResult = strtok(strCopy, delims);
// +1 for the first slash, +1 for the second slash, + another for the terminating NULL
char *finalResult = (char*)calloc(strlen(tmpResult) + 3, 1);
strcat(finalResult, "/");
strcat(finalResult, tmpResult);
strcat(finalResult, "/");
free(strCopy);
printf("%s",finalResult);
}
输出:
/709/
使用 strchr 查找第一个斜杠。前进指针并找到第二个斜线。将指针前进并设置为 '[=11=]'
.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main (int argc , char *argv[]) {
char *tok;
char text[] = "/709/nvitemp1/d/s/";
if ( ( tok = strchr ( text, '/')) != NULL) {//find first /
tok++;
if ( ( tok = strchr ( tok, '/')) != NULL) {//find second /
tok++;
*tok = '[=10=]';
printf ( "%s\n", text);
}
}
return 0;
}
您可以这样尝试:
char str[100] = "/709/nviTemp1";
char resu[100];
char *tmp;
tmp = strchr(str+1, '/');
strncpy(resu, str, (size_t)(tmp - str) + 1);
resu[(size_t)(tmp - str) + 1] = '[=10=]';
strchr
搜索第一个 '/',但从 str+1
开始会跳过真正的第一个。然后在开始和找到'/'之间计算"size"并使用strncpy
复制内容,并添加尾随'\0'。