如果我们有多个分隔符,如何提取字符串? [英] How to extract the string if we have have more than one delimiters?
问题描述
如果我有这个代码:
int main ()
{
char *sentence = "Wisteria#Tunnel";
char stringA[50];
char stringB[50];
char stringC[50];
pDelim = strstr(sentence,"#");
*pDelim = '\0';
strcpy(stringA,sentence);
strcpy(stringB,(pDelim+1));
return(0);
}
运行后,stringA
会是Wisteria",stringB
会是Tunnel",对吧?
After running it, stringA
will be "Wisteria" and stringB
will be "Tunnel", right?
如果:
char *sentence = "Wisteria#Tunnel#Japan";
如何将其提取为三部分,stringA
为Wisteria",stringB
为Tunnel",stringC
为日本"
How to extracted it to three part , stringA
will be "Wisteria" , stringB
will be "Tunnel" and stringC
will be "Japan"
谢谢.
推荐答案
标准字符串函数 strtok
、strstr
、strchr
都可以工作,前提是周围的代码也是正确的.也就是说,如果您想在同一个源字符串中多次搜索,您需要在源的开头指向一个 Start-at 指针,并且在每次(成功!)搜索后将其更新为指向 after 你的分隔符.默认情况下, strtok
可以很好地满足您的目的——单个字符分隔符.您可以在任何好的参考资料中阅读 strtok
(我不会为您查找).
Standard string functions strtok
, strstr
, strchr
all will work, provided the surrounding code is correct as well. That is, if you want to search multiple times in the same source string, you need to point a Start-at pointer at the start of your source, and after each (succesful!) search update it to point after your delimiter. By default, strtok
will work fine for your purpose -- a single character delimiter. You can read about strtok
in any good reference (I'm not going to look it up for you).
其实就是这么简单的操作,我刚才编的.除了普通的 strtok
,我的 strcpyUpTo
接受任何分隔符字符串.我本可以使用 strstr
来首先检查,但我更喜欢立即复制.复制循环中的 strncmp
和 strlen
可能效率低下;话又说回来,一般来说,这些低级字符串操作应该还是很快的.
In fact it is such a simple operation, I made this up just now. Other than vanilla strtok
, my strcpyUpTo
accepts any delimiter string. I could have used strstr
to first check, but I prefer copying right away. The strncmp
and strlen
inside the copy loop may be inefficient; then again, in general these low-level string operations should be pretty fast nevertheless.
#include <stdio.h>
#include <string.h>
char *strcpyUpTo (const char *source, char *dest, const char *delim)
{
while (*source && strncmp (source, delim, strlen(delim)))
*dest++ = *source++;
*dest = 0;
if (*source)
return (char *)(source+strlen(delim));
return (char *)source;
}
int main (void)
{
char *sentence = "Wisteria#any#Tunnel#any#Japan";
char stringA[50];
char stringB[50];
char stringC[50];
char *pDelim;
pDelim = strcpyUpTo (sentence, stringA, "#any#");
pDelim = strcpyUpTo (pDelim, stringB, "#any#");
pDelim = strcpyUpTo (pDelim, stringC, "#any#");
printf ("stringA = \"%s\"\n", stringA);
printf ("stringB = \"%s\"\n", stringB);
printf ("stringC = \"%s\"\n", stringC);
return 0;
}
<小时>
补充
对于未知数量的子字符串,您可以使用 while
循环,如下所示.(对于生产代码,它需要各种检查.为了简洁起见,为了清晰起见,并作为练习给读者.)这基本上也是您使用 strtok
的方式.
Addition
For an unknown number of substrings, you can use a while
loop such as the following. (For production code, it needs all kinds of checks. Left out for brevity, for clarity, and as exercise for the reader.) It's basically how you'd use strtok
as well.
char resultList[10][50];
int i, n_result;
pDelim = sentence;
n_result = 0;
do
{
pDelim = strcpyUpTo (pDelim, resultList[n_result], "#any#");
n_result++;
} while (*pDelim && n_result < 10);
printf ("number of strings: %d\n", n_result);
for (i=0; i<n_result; i++)
printf ("string %d = \"%s\"\n", i, resultList[i]);
这篇关于如果我们有多个分隔符,如何提取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!